Skip to content

Instantly share code, notes, and snippets.

Avatar

Ben Broide BenBroide

  • New York
View GitHub Profile
View custom-remove-duplicate-buttons.php
<?php
/**
* Plugin Name: Gutenberg Easy Clone/Remove block buttons
*/
function create_block_custom_remove_duplicate_buttons_block_init() {
$dir = __DIR__;
$script_asset_path = "$dir/build/index.asset.php";
if ( ! file_exists( $script_asset_path ) ) {
View index.js
import {registerPlugin} from '@wordpress/plugins';
import {select, dispatch} from '@wordpress/data';
import {BlockControls} from '@wordpress/block-editor';
import {ToolbarButton, ToolbarGroup} from '@wordpress/components';
import {trash, group} from '@wordpress/icons';
const CloneButton = () => (<ToolbarButton
icon={group}
onClick={cloneSelectedBlocks}
label={'Clone Block'}
View gist:a829ccb6b08d26db55d5172147749259
<?php
add_filter( 'sgf_register_fields', 'sgf_post_fields' );
// Register operator fields
function sgf_post_fields( $fields_array ) {
//Simple text field
$fields_array[] = [
'meta_key' => 'publisher',
];
View MediaUpload.js
import {MediaUpload, MediaUploadCheck} from '@wordpress/block-editor';
const {Button} = wp.components
const {dispatch, useSelect} = wp.data
const ALLOWED_MEDIA_TYPES = ['image'];
const ImagePlaceholder = () => (
<div style={{
width: '100%',
View RepeaterControl.js
import controlsIndex from "./controlsIndex";
const {select, withDispatch, useSelect} = wp.data
const InnerControlComponent = props => {
const {key, field, row_index, property_key, repeater_record_label} = props
let ControlField = controlsIndex['text']
let repeaterValues = select('core/editor').getEditedPostAttribute('meta')?.[props.meta_key]
View TextControl.js
const {withSelect, select, withDispatch, useSelect} = wp.data
const {TextControl} = wp.components
const ControlField = withSelect(
(select, props) => {
const { label, meta_key} = props.field;
const {row_index,property_key} = props
const value = select('core/editor').getEditedPostAttribute('meta')[meta_key];
const key = meta_key + row_index + property_key;
View CotrolsIndex.js
import TextField from "./TextControl";
import ColorPickerComponent from "./ColorPicker";
import SelectControlComponent from "./SelectControl";
import MediaUpload from "./MediaUpload";
import RepeaterControl from "./RepeaterControl";
const controlsIndex =
{
text: TextField,
color: ColorPickerComponent,
View index.js
import {registerPlugin} from '@wordpress/plugins';
import {PluginDocumentSettingPanel} from '@wordpress/edit-post';
import TextFieldHoc from './TextControlHoc'
import SelectControlHoc from './SelectControlHoc'
import ColorPickerHoc from './ColorPickerHoc'
import MediaUploadHoc from './MediaUploadHoc'
import RepeaterControlHoc from './RepeaterControlHoc'
const controlsIndex =
{
View sgf_load_scripts.php
<?php
function sgf_load_scripts() {
$dir = __DIR__;
$script_asset_path = "$dir/build/index.asset.php";
if ( ! file_exists( $script_asset_path ) ) {
throw new Error(
'You need to run `npm start` or `npm run build` for the "create-block/simple-guten-fields" block first.'
);
}
View sgf_meta_fields.php
<?php
function sgf_meta_fields() {
$fields_array = apply_filters( 'sgf_register_fields', [] );
foreach ( $fields_array as $field ) {
// Ensure post type exists and field name is valid
if ( ! $field['post_type'] || ! post_type_exists( $field['post_type'] ) || ! $field['meta_key'] || ! is_string( $field['meta_key'] ) ) {
return;
}
// Using Null Coalesce Operator to set defaults