Block Bindings - markhowellsmead/helpers GitHub Wiki

Modifying the content of a paragraph

Block metadata

<!-- wp:paragraph {
            "metadata":{
                "bindings":{
                    "content":{
                        "source":"shp2024/current-date"
                    }
                }
            }
        } -->
    <p>CURRENT DATE</p>
<!-- /wp:paragraph -->

Modifying the URL of a button block

<!-- wp:buttons -->
    <div class="wp-block-buttons">
        <!-- wp:button {
            "metadata":{
                "bindings":{
                    "url":{
                        "source":"shp2024/post-type-archive-link"
                    }
                }
            }
        } -->
        <div class="wp-block-button"><a class="wp-block-button__link wp-element-button" href="#">zurück</a></div>
        <!-- /wp:button -->
    </div>
<!-- /wp:buttons -->

The PHP

<?php

namespace SayHello\MustUsePlugin\Controller;

class BlockBindings
{
	public function run()
	{
		add_action('init', [$this, 'registerSources']);
		add_action('init', [$this, 'currentDate']);
	}

	public function registerSources()
	{
		register_block_bindings_source('shp2024/post-type-archive-link', array(
			'label' => __('Post Type Archive Link', 'shp-2024'),
			'get_value_callback' => [$this, 'postTypeArchiveLink'],
		));

		register_block_bindings_source('shp2024/current-data', array(
			'label' => __('Current date', 'shp-2024'),
			'get_value_callback' => [$this, 'currentDate'],
		));
	}

	public function postTypeArchiveLink($source_args, $block)
	{

		$post_type = get_post_type();
		$post_type_link = get_post_type_archive_link($post_type);

		if (empty($post_type) || empty($post_type_link)) {
			return null;
		}

		return $post_type_link;
	}

	public function currentDate()
	{
		return date_i18n(get_option('date_format'));
	}
}
⚠️ **GitHub.com Fallback** ⚠️