{"id":46496,"date":"2026-09-25T19:01:41","date_gmt":"2026-09-25T19:01:41","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/?p=46496"},"modified":"2026-10-10T21:01:06","modified_gmt":"2026-10-10T21:01:06","slug":"custom-block-bindings-source-post-data","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/custom-block-bindings-source-post-data\/","title":{"rendered":"Custom Block Bindings Source for Post Data in WordPress"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">You want a \u201cLast updated\u201d date or an author link inside a single post template. The Post Date and Post Author blocks don\u2019t fit because you need the value inside a styled Paragraph, a Heading or a Button, next to your own design. Building a custom block for one line of text is overkill, and shortcodes don\u2019t belong in block templates.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/developer.wordpress.org\/block-editor\/reference-guides\/block-api\/block-bindings\/\">Block Bindings API<\/a> solves this. You register a source once in PHP. Then any Paragraph, Heading or Button can pull its text or URL from that source, and it keeps all of its normal styling controls. The snippet below adds a <code>dpc\/post-data<\/code> source with four keys: <code>modified<\/code>, <code>modified_iso<\/code>, <code>author<\/code> and <code>author_url<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The snippet<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Put this in a small plugin, an mu-plugin, or your theme\u2019s <code>functions.php<\/code>. It needs WordPress 6.5 or newer. For the editor to show the source label correctly, use 6.7 or newer.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\nadd_action( 'init', 'dpc_register_post_data_binding' );\n\nfunction dpc_register_post_data_binding(): void {\n\tif ( ! function_exists( 'register_block_bindings_source' ) ) {\n\t\treturn;\n\t}\n\n\tregister_block_bindings_source(\n\t\t'dpc\/post-data',\n\t\tarray(\n\t\t\t'label'              =&gt; __( 'Post data', 'dpc' ),\n\t\t\t'get_value_callback' =&gt; 'dpc_post_data_binding_value',\n\t\t\t'uses_context'       =&gt; array( 'postId' ),\n\t\t)\n\t);\n}\n\nfunction dpc_post_data_binding_value( array $source_args, WP_Block $block, string $attribute_name ): ?string {\n\t$key     = isset( $source_args&#91;'key'&#93; ) ? sanitize_key( $source_args&#91;'key'&#93; ) : '';\n\t$post_id = $block-&gt;context&#91;'postId'&#93; ?? get_the_ID();\n\t$post    = $post_id ? get_post( (int) $post_id ) : null;\n\n\tif ( ! $post ) {\n\t\treturn null;\n\t}\n\n\t\/\/ Never leak data from drafts or private posts to visitors who cannot read them.\n\tif ( ! is_post_publicly_viewable( $post ) &amp;&amp; ! current_user_can( 'read_post', $post-&gt;ID ) ) {\n\t\treturn null;\n\t}\n\n\tswitch ( $key ) {\n\t\tcase 'modified':\n\t\t\treturn esc_html( get_the_modified_date( '', $post ) );\n\n\t\tcase 'modified_iso':\n\t\t\treturn esc_html( get_the_modified_date( 'c', $post ) );\n\n\t\tcase 'author':\n\t\t\treturn esc_html( get_the_author_meta( 'display_name', (int) $post-&gt;post_author ) );\n\n\t\tcase 'author_url':\n\t\t\treturn esc_url( get_author_posts_url( (int) $post-&gt;post_author ) );\n\t}\n\n\t\/**\n\t * Let other code add keys. Return an escaped string, or null to keep the block's own content.\n\t *\/\n\t$value = apply_filters( 'dpc_post_data_binding_value', null, $key, $post, $attribute_name );\n\n\treturn is_string( $value ) ? $value : null;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Add it with Scripts Organizer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/dplugins.com\/downloads\/scripts-organizer\/\">Scripts Organizer<\/a> go to <strong>Code Blocks \u2192 Add New<\/strong> and give the block a name. Set <strong>Trigger location<\/strong> to <strong>Everywhere<\/strong>, tick <strong>PHP<\/strong> under <strong>Script location<\/strong>, paste the code into the PHP editor and publish. Leave <strong>Action\/Hook<\/strong> empty.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Everywhere<\/strong> matters here: bound blocks are rendered on the front end, in REST requests and in the Site Editor preview, and the source has to be registered on <code>init<\/code> in all of them.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Use it in a template<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Block Bindings don\u2019t have a full UI for custom sources yet, so add the binding in the Code editor view or directly in your template or pattern markup. The text inside the block is the fallback. It shows when the source returns <code>null<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"html\" class=\"language-html\">&lt;!-- wp:paragraph {\"metadata\":{\"bindings\":{\"content\":{\"source\":\"dpc\/post-data\",\"args\":{\"key\":\"modified\"}}}}} --&gt;\n&lt;p&gt;Last updated&lt;\/p&gt;\n&lt;!-- \/wp:paragraph --&gt;\n\n&lt;!-- wp:buttons --&gt;\n&lt;div class=\"wp-block-buttons\"&gt;&lt;!-- wp:button {\"metadata\":{\"bindings\":{\"text\":{\"source\":\"dpc\/post-data\",\"args\":{\"key\":\"author\"}},\"url\":{\"source\":\"dpc\/post-data\",\"args\":{\"key\":\"author_url\"}}}}} --&gt;\n&lt;div class=\"wp-block-button\"&gt;&lt;a class=\"wp-block-button__link wp-element-button\" href=\"#\"&gt;Author&lt;\/a&gt;&lt;\/div&gt;\n&lt;!-- \/wp:button --&gt;\n&lt;\/div&gt;\n&lt;!-- \/wp:buttons --&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How it works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>register_block_bindings_source()<\/code> registers <code>dpc\/post-data<\/code>. <code>uses_context<\/code> asks WordPress to pass <code>postId<\/code> to every block that uses the source. This makes it work inside a Query Loop as well as on single templates.<\/li>\n\n\n<li>When a bound block renders, core calls <code>dpc_post_data_binding_value()<\/code> for each bound attribute. It then puts the returned value into the block\u2019s HTML: the inner text for <code>content<\/code> and <code>text<\/code>, and the <code>href<\/code> for <code>url<\/code>.<\/li>\n\n\n<li>Returning <code>null<\/code> tells core to leave the block unchanged, so your fallback text shows instead.<\/li>\n\n\n<li>The visibility check stops a bound block from exposing the author or date of a private or draft post to someone who cannot read that post.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Customise it<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add your own keys without editing the function. Hook <code>dpc_post_data_binding_value<\/code> and return an escaped string, in the same code block below the snippet:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">add_filter( 'dpc_post_data_binding_value', 'dpc_post_data_word_count', 10, 4 );\n\nfunction dpc_post_data_word_count( ?string $value, string $key, WP_Post $post, string $attribute_name ): ?string {\n\tif ( 'word_count' !== $key ) {\n\t\treturn $value;\n\t}\n\n\t$words = str_word_count( wp_strip_all_tags( $post-&gt;post_content ) );\n\n\treturn esc_html( sprintf( _n( '%s word', '%s words', $words, 'dpc' ), number_format_i18n( $words ) ) );\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Only certain attributes can be bound: Paragraph <code>content<\/code>, Heading <code>content<\/code>, Button <code>text<\/code>\/<code>url<\/code>\/<code>linkTarget<\/code>\/<code>rel<\/code>, and Image <code>id<\/code>\/<code>url<\/code>\/<code>alt<\/code>\/<code>title<\/code>. Binding any other attribute does nothing.<\/li>\n\n\n<li>The binding replaces the whole text of the block. To show a label like \u201cUpdated:\u201d, put it in its own Paragraph and place both paragraphs in a Row group.<\/li>\n\n\n<li>In the editor, a block bound to a server-only source shows the source label (\u201cPost data\u201d) and can\u2019t be edited. The real value only appears on the front end. For a live preview in the editor, register a matching source in JavaScript with <code>registerBlockBindingsSource()<\/code> and a <code>getValues<\/code> callback.<\/li>\n\n\n<li>Always escape inside the callback. Core runs rich-text values through <code>wp_kses_post()<\/code>, but it doesn\u2019t know which values should be plain text.<\/li>\n\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>Register a WordPress Block Bindings source that fills core Paragraph, Heading and Button blocks with a post\u2019s last-updated date and author.<\/p>\n","protected":false},"author":1,"featured_media":46888,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Custom Block Bindings Source for Post Data in WordPress","description":"Register a WordPress Block Bindings source that fills core Paragraph, Heading and Button blocks with a post\u2019s last-updated date and author."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[90,3],"tags":[137,140,138,121,139],"dpc_library":[204],"class_list":["post-46496","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-gutenberg","category-wordpress","tag-block-bindings","tag-block-themes","tag-core-blocks","tag-gutenberg","tag-php"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46496","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/comments?post=46496"}],"version-history":[{"count":2,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46496\/revisions"}],"predecessor-version":[{"id":46887,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46496\/revisions\/46887"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46888"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=46496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/categories?post=46496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=46496"},{"taxonomy":"dpc_library","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/dpc_library?post=46496"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}