Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use allowed_block_types_all filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
July 12, 2022
5 minutes read

allowed_block_types_all filter

This filter is used to minimize blocks available for editing. By utilizing this filter, developers can carefully curate and customize the selection of available blocks, tailoring the editing experience to better suit the specific needs and preferences of users. This filter enables a more focused content creation process, ensuring that users have access to only the essential blocks required for their particular use case while minimizing unnecessary complexity.

As illustrated in the screenshots below, the left side displays the full array of available blocks, whereas the right side showcases a refined selection achieved by applying this filter.

all_block_types limited_block_types

To use allowed_block_types_all filter, first you have to register it using add_filter. You can write this code into functions.php of your activated theme or in a custom WordPress Plugin.

We at WePlugins, (formerly Flipper Code), always prefer to create a custom WordPress Plugin while using hooks so nothing breaks when you update your WordPress Theme in the future.

In the below live example, we have defined a function modify_allowed_block_types_all_defaults which takes 2 parameters and we registered using add_filter. The first parameter allowed_block_types_all is name of the hook, The second parameter modify_allowed_block_types_all_defaults is name of the function which need to be called, third parameter is the priority of calling the hook if same hook is used multiple times and the last parameter is the number of arguments (if any) to be passed in the registered function.

Sometime, you have to remove a registered hook so you can use remove_filter to remove allowed_block_types_all filter.

Parameters

    Below are the 2 parameters are required to use this hook.

  • $allowed_block_types : (bool|string[]) Array of block type slugs, or boolean to enable/disable all. Default true (all registered block types supported).
  • $block_editor_context : (WP_Block_Editor_Context) The current block editor context.

Live Example

Below is an example how you can use this hook.

                        function modify_allowed_block_types_all_defaults($allowed_block_types, $block_editor_context) { 
   
                            // Update the $allowed_block_types variable according to your website requirements and return this variable. You can modify the $allowed_block_types variable conditionally too if you want.

                            return $allowed_block_types; 
                        }
                        // add the filter
                        add_filter( "allowed_block_types_all", "modify_allowed_block_types_all_defaults", 10, 2 );

Limiting to Core Paragraph,Heading and List Blocks

function wpdocs_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', 'wpdocs_allowed_block_types', 10, 2 );

Customizing Block Types Based on User Roles

function customize_blocks_for_user_roles($allowed_blocks) {
    $current_user = wp_get_current_user();
    
    if (in_array('editor', $current_user->roles)) {
        // Editors can use additional blocks
        $allowed_blocks[] = 'core/gallery';
    }
    
    return $allowed_blocks;
}
add_filter('allowed_block_types_all', 'customize_blocks_for_user_roles');

To remove a hook callback, use the example below.

remove_filter( "allowed_block_types_all", "modify_allowed_block_types_all_defaults", 10, 2 );

Please make sure provide the same callback function name, priority and number of arguments while removing the hook callback.

WePlugins, (formerly Flipper Code) is a Premium WordPress Plugins development company and integrating new functionalities into WordPress sites in form of custom WordPress Plugins since 2012. If you’re having any trouble using this hook, please contact our WordPress Development Team and we’d be happy 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.