WP_Block_Bindings_Source

Represents a registered block bindings source.

Source: wp-includes/class-wp-block-bindings-source.php
Since: 6.5.0
Access: private (internal use by registry)

Overview

This class is created internally by WP_Block_Bindings_Registry::register(). Do not instantiate directly; use register_block_bindings_source() instead.

Properties

PropertyTypeVisibilityDescription
$namestringpublicSource name (e.g., core/post-meta)
$labelstringpublicHuman-readable label
$get_value_callbackcallableprivateCallback to retrieve values
$uses_contextstring[]|nullpublicRequired block context keys

Methods

__construct()

Creates a new source instance.

php
public function __construct( string $name, array $source_properties )
ParameterTypeDescription
$namestringSource name
$source_propertiesarrayConfiguration properties

Note: Do not use directly. Use register_block_bindings_source() or WP_Block_Bindings_Registry::register().

Example (internal usage):

php
$source = new WP_Block_Bindings_Source( 'my-plugin/data', array(
    'label'              => 'My Data',
    'get_value_callback' => 'my_callback_function',
    'uses_context'       => array( 'postId' ),
) );

get_value()

Retrieves a value from the source for a given block attribute.

php
public function get_value( array $source_args, WP_Block $block_instance, string $attribute_name ): mixed
ParameterTypeDescription
$source_argsarrayArguments from block’s binding configuration
$block_instanceWP_BlockThe block being rendered
$attribute_namestringTarget attribute name

Returns: The computed value for the source (filtered by block_bindings_source_value).

Process:

  1. Calls $get_value_callback with provided arguments
  2. Applies block_bindings_source_value filter (since 6.7.0)
  3. Returns filtered value

Example:

php
$source = get_block_bindings_source( 'core/post-meta' );

$value = $source->get_value(
    array( 'key' => 'custom_field' ),
    $block_instance,
    'content'
);

__wakeup()

Prevents unserialization (security hardening).

php
public function __wakeup(): void

Throws: LogicException — sources should never be unserialized.


Usage Context

The uses_context property declares which block context values the source needs. When a source is used, WordPress ensures these context values are available to the block.

Common Context Keys

Context KeyDescription
postIdCurrent post ID
postTypeCurrent post type
termIdCurrent term ID (6.9.0+)
taxonomyCurrent taxonomy (6.9.0+)
pattern/overridesPattern override data

Example

php
register_block_bindings_source( 'my-plugin/related-post', array(
    'label'              => 'Related Post',
    'get_value_callback' => function( $args, $block, $attr ) {
        $post_id = $block->context['postId'] ?? null;
        if ( ! $post_id ) {
            return null;
        }
        // Use post_id to find related content
        return get_related_post_title( $post_id );
    },
    'uses_context' => array( 'postId', 'postType' ),
) );