Skip to content

Instantly share code, notes, and snippets.

@codersantosh
Created May 9, 2024 20:26
Show Gist options
  • Select an option

  • Save codersantosh/34c750ac124ae56e6f4f029e17b0807c to your computer and use it in GitHub Desktop.

Select an option

Save codersantosh/34c750ac124ae56e6f4f029e17b0807c to your computer and use it in GitHub Desktop.
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: {
content: {
source: 'prefix-project/prefix-data',
args: {
key: 'prefix_data',
},
},
},
},
placeholder: __('Prefix data', 'text-domain'),
},
example: {},
isActive: (blockAttributes) =>
'prefix_data' === blockAttributes?.metadata?.bindings?.content?.args?.key,
});
<?php
/**
* Custom source for the block bindings.
*
* @since 1.0.0
* @package Prefix Project
* @subpackage Block Bindings
*/
add_action( 'init', 'prefix_project_register_block_bindings' );
/**
* Registers prefix-project/prefix-data source in the block bindings registry.
*
* @since 1.0.0
*/
function prefix_project_register_block_bindings() {
register_block_bindings_source(
'prefix-project/prefix-data',
array(
'label' => __( 'Project data', 'text-domain' ),
'get_value_callback' => 'prefix_project_binding_get_data',
'uses_context' => array( 'postId', 'postType' ),
)
);
}
/**
* Gets value of project data.
*
* @since 1.0.0
*
* @param array $source_args Array containing source arguments used to look up the override value.
* Example: array( "key" => "foo" ).
* @param WP_Block $block_instance The block instance.
* @return mixed The value computed for the source.
*/
function prefix_project_binding_get_data( array $source_args, $block_instance ) {
// for now just returning testing text
return esc_html__( 'Hello from binding data', 'text-domain' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment