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 the two seemed impossible.
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.
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.
function custom_load_blocks(){
global $pagenow;
$blocks_to_register = array(
'home-banner',
'home-intro',
'custom-gallery'
);
$blacklist_block_types = array();
// Check if this is the intended custom post type
if(is_admin() && in_array($pagenow,array('post-new.php','post.php'))){
if (isset($_GET['post'])) {
$post_id = (int)$_GET['post'];
} elseif (isset($_POST['post_ID'])) {
$post_id = (int)$_POST['post_ID'];
}
// If the post_id is set and it matches the front page, set the blacklist_block_types
if($post_id !== (int)get_option('page_on_front')){
if(get_post_type($post_id) != 'acf-field-group'){
$blacklist_block_types = array(
'home-banner',
'home-intro'
);
}
}
}
$blocks_to_register = array_diff($blocks_to_register, $blacklist_block_types);
foreach ($blocks_to_register as $btr) {
register_block_type(__DIR__ . '/blocks/' . $btr . '/block.json');
}
}
add_action('init', 'custom_load_blocks');
Here’s a rundown of the code in the above function:
function custom_load_blocks(){
global $pagenow;
The custom_load_blocks()
function is defined with global $pagenow;
set as a global variable. This variable helps to determine the current page of the admin area where the custom blocks should be displayed.
$blocks_to_register = array(
'home-banner',
'home-intro',
'custom-gallery'
);
An array of custom blocks to register is created. The blocks in this example are ‘home-banner’, ‘home-intro’, and ‘custom-gallery’. Additional blocks can be added to the array as needed.
$blacklist_block_types = array();
An empty array is created to hold any block types that should not be displayed.
if(is_admin() && in_array($pagenow,array('post-new.php','post.php'))){
This line checks if the user is an admin and if they are on the ‘post-new.php’ or ‘post.php’ pages. This ensures that the custom blocks are only registered when a post is being created or edited.
if (isset($_GET['post'])) {
$post_id = (int)$_GET['post'];
} elseif (isset($_POST['post_ID'])) {
$post_id = (int)$_POST['post_ID'];
}
This section determines the post ID being edited. The $_GET['post']
and $_POST['post_ID']
parameters are used to extract the post ID being edited.
if($post_id !== (int)get_option('page_on_front')){
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.
if(get_post_type($post_id) != 'acf-field-group'){
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.
$blacklist_block_types = array(
'home-banner',
'home-intro'
);
An array of block types is created to blacklist the custom blocks that should not be displayed. In this example, ‘home-banner’ and ‘home-intro’ are blocked. This is because they are only intended to be used on the front page of the website.
$blocks_to_register = array_diff($blocks_to_register, $blacklist_block_types);
The $blacklist_block_types
array is removed from the $blocks_to_register
array. This ensures that only the allowed custom blocks are registered.
foreach ($blocks_to_register as $btr) {
register_block_type(__DIR__ . '/blocks/' . $btr . '/block.json');
}
The `register_block_type()
` function is called for each custom block that is allowed to be registered. The `__DIR__
` 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.
add_action('init', 'custom_load_blocks');
The custom_load_blocks()
function is added as an action to the init
hook. This ensures that the function is called during the initialization of the WordPress core.
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.
Like what you’ve read, then why not tell others about it... they might enjoy it too
If you think Bronco has the skills to take your business forward then what are you waiting for?
Get in Touch Today!
Discussion