Skip to content

Instantly share code, notes, and snippets.

View codersantosh's full-sized avatar
💭
React + Gutenberg + API + Database

Santosh Kunwar codersantosh

💭
React + Gutenberg + API + Database
View GitHub Profile
@codersantosh
codersantosh / wp-plugin-pattern.php
Last active November 4, 2024 09:48
Automatic registration of patterns within the "patterns" folder for active plugins, similar to how it works for themes.
<?php // phpcs:ignore Class file names should be based on the class name with "class-" prepended.
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WP_Plugin_Pattern Class
*
* Handles the management of block patterns for the plugin.
@codersantosh
codersantosh / block-bindings.php
Created September 20, 2024 18:48
Archive title block bindings
/**
* Register block binding sources.
*/
if ( ! function_exists( 'prefix_register_block_bindings' ) ) :
/**
* Register the archive title binding source.
*
* @since Project Name 1.0.0
* @return void
*/
@codersantosh
codersantosh / install_and_activate_plugin.php
Last active July 17, 2024 15:24
Programmatically install and activate WordPress plugins
/**
* Programmatically install and activate WordPress plugins.
* Supports installation of plugins from the WordPress plugin repository and external sources via ZIP files.
*/
if ( ! function_exists( 'prefix_is_plugin_active' ) ) {
/**
* Checks if a given plugin is active.
*
* @since 1.0.0
@codersantosh
codersantosh / editor.js
Created May 9, 2024 20:26
Custom source for the block bindings.
registerBlockVariation('core/paragraph', {
name: 'prefix-project/prefix-field',
title: __('Prefix field', 'text-domain'),
description: __('Displays the Prefix field.', 'text-domain'),
category: 'text',
keywords: ['prefix', 'project', 'field'],
scope: ['inserter'],
attributes: {
metadata: {
bindings: {
@codersantosh
codersantosh / AbcUseDelayFunction.js
Last active January 13, 2023 15:52
The AbcUseDelayFunction is a custom hook that allows you to delay the execution of a given function. It takes two arguments, the first argument is the function that you want to delay, and the second argument is the delay time in milliseconds (defaults to 2000ms).
/*WordPress*/
import {
useEffect,
useRef,
useState
} from "@wordpress/element";
/*Library*/
import {
cloneDeep
@codersantosh
codersantosh / path-to-api-media.js
Created August 17, 2022 10:09
Upload file from React/Gutenberg on WordPress
import apiFetch from "@wordpress/api-fetch";
export const fetchMedia = async (id) => {
let path = 'wp/v2/media/'+id,
media ={};
try {
media = await apiFetch({
path: path,
method : 'GET'
@codersantosh
codersantosh / 1. render-css.js
Last active June 28, 2022 15:08
React Component for Internal CSS
/*STYLIS : https://www.npmjs.com/package/stylis*/
import {compile, serialize, stringify, middleware, prefixer} from 'stylis';
/*Process the Raw CSS*/
const getProcessedCss = (rawCss) =>{
return serialize(compile(rawCss), middleware([prefixer, stringify]))
}
/*Render CSS inside style tag*/
const RenderCss = (props) => {
@codersantosh
codersantosh / wp-remove-all-image-sizes.php
Last active March 10, 2022 12:52
Only allow original uploaded image and Remove all image sizes generated by WordPress.
/*Remove all image sizes*/
function prefix_remove_image_sizes( $sizes ) {
return array();
}
add_action( 'intermediate_image_sizes_advanced', 'prefix_remove_image_sizes' );
/*Removed scaled image size (-scaled as suffix on image name)*/
add_filter( 'big_image_size_threshold', '__return_false' );
/**
* Get Post/Post Type Count in the given Taxonomy
*
* @param $post_type string any post type
* @param $taxonomy string any taxonomy
* @param $is_exists string NOT EXISTS or EXISTS
*
* @return String Number of Post count
*/
<?php
/**
* Add Category Dynamic Css
*
* Edit the selector on the function
* Change Selector as your theme/plugin
*/
if ( ! function_exists( 'prefix_get_post_categories_color' ) ) {
function prefix_get_post_categories_color( $imp = false) {