This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
As a fellow developer, I totally get it – customizing WordPress to fit our unique needs is a journey! The allowed_block_types_all filter is a fantastic tool for tailoring the editing experience in WordPress. By leveraging this filter, we can narrow down the blocks available for editing, giving users precisely what they need and nothing more. This makes content creation more focused and less cluttered, which is a win-win!
Check out these screenshots below. On the left, you’ll see all the available blocks, while on the right, there’s a refined selection achieved by applying this filter.
To use the allowed_block_types_all filter, you need to register it using add_filter
. You can place this code in the functions.php
file of your active theme or in a custom WordPress Plugin. Here at WePlugins, we always recommend creating a custom WordPress Plugin for using hooks, ensuring that nothing breaks when you update your WordPress Theme in the future.
Live Example 1: Basic Implementation
Here’s a straightforward example of how you can use this hook.
function weplugins_modify_allowed_block_types_all_defaults($allowed_block_types, $block_editor_context) { return $allowed_block_types; } add_filter("allowed_block_types_all", "weplugins_modify_allowed_block_types_all_defaults", 10, 2);
Live Example 2: Limiting to Core Paragraph, Heading, and List Blocks
function weplugins_allowed_block_types($block_editor_context, $editor_context) { if (!empty($editor_context->post)) { return array( 'core/paragraph', 'core/heading', 'core/list', ); } return $block_editor_context; } add_filter('allowed_block_types_all', 'weplugins_allowed_block_types', 10, 2);
Live Example 3: Customizing Block Types Based on User Roles
function weplugins_customize_blocks_for_user_roles($allowed_blocks) { $current_user = wp_get_current_user(); if (in_array('editor', $current_user->roles)) { $allowed_blocks[] = 'core/gallery'; } return $allowed_blocks; } add_filter('allowed_block_types_all', 'weplugins_customize_blocks_for_user_roles');
And if you need to remove a hook callback, here’s how you can do it:
remove_filter("allowed_block_types_all", "weplugins_modify_allowed_block_types_all_defaults", 10, 2);
Make sure to provide the same callback function name, priority, and number of arguments when removing the hook callback.
Contact Us
If you need any help with customization or have any queries, feel free to reach out to us at WePlugins. We’re here to assist you!
Explore the latest in WordPress
Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.