{"id":12035,"date":"2023-03-31T10:43:46","date_gmt":"2023-03-31T09:43:46","guid":{"rendered":"https:\/\/www.bronco.co.uk\/our-ideas\/?p=12035"},"modified":"2023-03-27T11:13:35","modified_gmt":"2023-03-27T10:13:35","slug":"enabling-custom-acf-blocks-for-specific-pages-only","status":"publish","type":"post","link":"https:\/\/www.bronco.co.uk\/our-ideas\/enabling-custom-acf-blocks-for-specific-pages-only\/","title":{"rendered":"Enabling Custom ACF Blocks for Specific Pages Only"},"content":{"rendered":"\n<p>Working with WordPress and finding the perfect balance between creating an engaging homepage and providing flexible editing options for clients can be challenging. <\/p>\n\n\n\n<p>Recently, I came across a situation where I wanted to use custom WordPress Blocks (using ACF) and traditional Advanced Custom Fields (ACF) inputs on the homepage of a new website. However, combining the two seemed impossible.<\/p>\n\n\n\n<p>Therefore, I had to find a solution where I could create the entire page using blocks, but in doing so I also wanted some of this blocks to be only accessible when editting the homepage. <\/p>\n\n\n\n<p>After experimenting with various options, the following function achieved the desired result of enabling me to creating custom ACF blocks that would be enabled only on specific pages.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>\nfunction custom_load_blocks(){\n\n\tglobal $pagenow;\t\n\n    $blocks_to_register = array(\n        &#39;home-banner&#39;,\n\t\t    &#39;home-intro&#39;,\n        &#39;custom-gallery&#39;\n    );\n\n    $blacklist_block_types = array();\n\n    \/\/ Check if this is the intended custom post type\n    if(is_admin() && in_array($pagenow,array(&#39;post-new.php&#39;,&#39;post.php&#39;))){\n\n        if (isset($_GET[&#39;post&#39;])) {\n            $post_id = (int)$_GET[&#39;post&#39;];\n        } elseif (isset($_POST[&#39;post_ID&#39;])) {\n            $post_id = (int)$_POST[&#39;post_ID&#39;];\n        }\n\n        \/\/ If the post_id is set and it matches the front page, set the blacklist_block_types\n        if($post_id !== (int)get_option(&#39;page_on_front&#39;)){\n\t\t\tif(get_post_type($post_id) != &#39;acf-field-group&#39;){\n\t\t\t\t$blacklist_block_types = array(\n\t\t\t\t\t&#39;home-banner&#39;,\n\t\t\t\t\t&#39;home-intro&#39;\n\t\t\t\t);\n\t\t\t}\n        }\n\n    }\n\n    $blocks_to_register = array_diff($blocks_to_register, $blacklist_block_types);\n\n    foreach ($blocks_to_register as $btr) {\n        register_block_type(__DIR__ . &#39;\/blocks\/&#39; . $btr . &#39;\/block.json&#39;);\n    }\n\n}\nadd_action(&#39;init&#39;, &#39;custom_load_blocks&#39;);<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">How does this work?<\/h2>\n\n\n\n<p>Here&#8217;s a rundown of the code in the above function:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>function custom_load_blocks(){\n\n\tglobal $pagenow;<\/code><\/pre><\/div>\n\n\n\n<p>The <code>custom_load_blocks()<\/code> function is defined with <code>global $pagenow;<\/code> set as a global variable. This variable helps to determine the current page of the admin area where the custom blocks should be displayed.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>$blocks_to_register = array(\n    &#39;home-banner&#39;,\n    &#39;home-intro&#39;,\n    &#39;custom-gallery&#39;\n);<\/code><\/pre><\/div>\n\n\n\n<p>An array of custom blocks to register is created. The blocks in this example are &#8216;home-banner&#8217;, &#8216;home-intro&#8217;, and &#8216;custom-gallery&#8217;. Additional blocks can be added to the array as needed.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>$blacklist_block_types = array();<\/code><\/pre><\/div>\n\n\n\n<p>An empty array is created to hold any block types that should not be displayed.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>if(is_admin() && in_array($pagenow,array(&#39;post-new.php&#39;,&#39;post.php&#39;))){<\/code><\/pre><\/div>\n\n\n\n<p>This line checks if the user is an admin and if they are on the &#8216;post-new.php&#8217; or &#8216;post.php&#8217; pages. This ensures that the custom blocks are only registered when a post is being created or edited.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>if (isset($_GET[&#39;post&#39;])) {\n    $post_id = (int)$_GET[&#39;post&#39;];\n} elseif (isset($_POST[&#39;post_ID&#39;])) {\n    $post_id = (int)$_POST[&#39;post_ID&#39;];\n}<\/code><\/pre><\/div>\n\n\n\n<p>This section determines the post ID being edited. The <code>$_GET['post']<\/code> and <code>$_POST['post_ID']<\/code> parameters are used to extract the post ID being edited.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>if($post_id !== (int)get_option(&#39;page_on_front&#39;)){<\/code><\/pre><\/div>\n\n\n\n<p>This line checks if the post being edited is not the front page of the website. If it is not the front page, then the custom blocks should not be displayed.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>if(get_post_type($post_id) != &#39;acf-field-group&#39;){<\/code><\/pre><\/div>\n\n\n\n<p>This line checks if the post type being edited is not an ACF field group. ACF field groups are used to define custom fields for pages, posts, and other content types. If the post being edited is an ACF field group, then the custom blocks should all be displayed.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>$blacklist_block_types = array(\n   &#39;home-banner&#39;,\n   &#39;home-intro&#39;\n);<\/code><\/pre><\/div>\n\n\n\n<p>An array of block types is created to blacklist the custom blocks that should not be displayed. In this example, &#8216;home-banner&#8217; and &#8216;home-intro&#8217; are blocked. This is because they are only intended to be used on the front page of the website.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>$blocks_to_register = array_diff($blocks_to_register, $blacklist_block_types);<\/code><\/pre><\/div>\n\n\n\n<p>The <code>$blacklist_block_types<\/code> array is removed from the <code>$blocks_to_register<\/code> array. This ensures that only the allowed custom blocks are registered.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>foreach ($blocks_to_register as $btr) {\n   register_block_type(__DIR__ . &#39;\/blocks\/&#39; . $btr . &#39;\/block.json&#39;);\n}<\/code><\/pre><\/div>\n\n\n\n<p>The `<code>register_block_type()<\/code>` function is called for each custom block that is allowed to be registered. The `<code>__DIR__<\/code>` constant is used to specify the path to the custom block. This line registers the custom block with the ACF plugin, making it accessible to the editor.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>add_action(&#39;init&#39;, &#39;custom_load_blocks&#39;);<\/code><\/pre><\/div>\n\n\n\n<p>The <code>custom_load_blocks()<\/code> function is added as an action to the <code>init<\/code> hook. This ensures that the function is called during the initialization of the WordPress core.<\/p>\n\n\n\n<p>The custom function makes it easy to register custom blocks in WordPress using the ACF plugin. By specifying the pages where the blocks should be displayed, editors can easily access and use the blocks without concern that certain blocks may not be suitable within the page being editted.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Working with WordPress and finding the perfect balance between creating an engaging homepage and providing flexible editing options for clients can be challenging. Recently, I came across a situation where I wanted to use custom WordPress Blocks (using ACF) and traditional Advanced Custom Fields (ACF) inputs on the homepage of a new website. However, combining [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":12036,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"class_list":["post-12035","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-and-ux"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts\/12035","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/comments?post=12035"}],"version-history":[{"count":0,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts\/12035\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/media\/12036"}],"wp:attachment":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/media?parent=12035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/categories?post=12035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}