{"id":144,"date":"2024-03-14T10:25:42","date_gmt":"2024-03-14T13:25:42","guid":{"rendered":"https:\/\/kylekelly.com\/?p=144"},"modified":"2024-04-16T17:14:52","modified_gmt":"2024-04-16T20:14:52","slug":"using-acf-with-wordpress-block-bindings-api","status":"publish","type":"post","link":"https:\/\/kylekelly.com\/posts\/using-acf-with-wordpress-block-bindings-api\/","title":{"rendered":"Using ACF with WordPress Block Bindings API"},"content":{"rendered":"\n
\n

Update 2024-04-02: ACF supports Block Bindings directly now with acf\/field<\/a><\/p>\n<\/div>\n\n\n\n

<\/p>\n\n\n\n

Advanced Custom Fields (ACF) doesn’t have direct support for Block Bindings yet (as of 2024-03-14), but in the meantime, this seems to work by creating a custom source for block bindings:<\/p>\n\n\n\n

add_action('init', function() {\n\tregister_block_bindings_source( 'namespace\/acf', array(\n\t\t'label'              => __( 'ACF Binding', 'namespace' ),\n\t\t'get_value_callback' => function(array $source_args, WP_Block $block_instance, string $attribute_name) {\n\t\t\tif(!isset($source_args['key'])) {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t\t$post_id = $block_instance->context['postId'];\n\t\t\treturn get_field($source_args['key'], $post_id);\n\t\t},\n\t\t'uses_context'       => [ 'postId', 'postType' ]\n\t) );\n});\t<\/code><\/pre>\n\n\n\n

and then reference in a template (use the code editor):<\/p>\n\n\n\n

<!-- wp:paragraph {\n\t\"metadata\":{\n\t\t\"bindings\":{\n\t\t\t\"content\":{\n\t\t\t\t\"source\":\"namespace\/acf\",\n\t\t\t\t\"args\":{\n\t\t\t\t\t\"key\":\"acf_field_name\"\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n} -->\n<p>title<\/p>\n<!-- \/wp:paragraph --><\/code><\/pre>\n\n\n\n

Reference: https:\/\/developer.wordpress.org\/news\/2024\/03\/06\/introducing-block-bindings-part-2-working-with-custom-binding-sources\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

Update 2024-04-02: ACF supports Block Bindings directly now with acf\/field Advanced Custom Fields (ACF) doesn’t have direct support for Block Bindings yet (as of 2024-03-14), but in the meantime, this seems to work by creating a custom source for block bindings: and then reference in a template (use the code editor): Reference: https:\/\/developer.wordpress.org\/news\/2024\/03\/06\/introducing-block-bindings-part-2-working-with-custom-binding-sources\/<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[4,9],"_links":{"self":[{"href":"https:\/\/kylekelly.com\/wp-json\/wp\/v2\/posts\/144"}],"collection":[{"href":"https:\/\/kylekelly.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kylekelly.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kylekelly.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kylekelly.com\/wp-json\/wp\/v2\/comments?post=144"}],"version-history":[{"count":7,"href":"https:\/\/kylekelly.com\/wp-json\/wp\/v2\/posts\/144\/revisions"}],"predecessor-version":[{"id":173,"href":"https:\/\/kylekelly.com\/wp-json\/wp\/v2\/posts\/144\/revisions\/173"}],"wp:attachment":[{"href":"https:\/\/kylekelly.com\/wp-json\/wp\/v2\/media?parent=144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kylekelly.com\/wp-json\/wp\/v2\/categories?post=144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kylekelly.com\/wp-json\/wp\/v2\/tags?post=144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}