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

How to use excerpt_allowed_blocks filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
June 10, 2023
5 minutes read

 

Working with WordPress hooks can be quite fascinating, especially when you’re looking to customize the functionality of your site without touching the core files. One such hook you might find yourself using is the excerpt_allowed_blocks filter. Let’s dive into how you can effectively use this hook in your projects.

Understanding the excerpt_allowed_blocks Filter

If you’re adding a dynamic block, it’s crucial that it doesn’t generate another excerpt to avoid an infinite loop. First, you need to register this filter using add_filter. It’s often recommended to do this in a custom WordPress Plugin, ensuring nothing breaks when updating your theme.

Example 1: Modify Allowed Blocks

This example demonstrates how to modify the allowed blocks using the excerpt_allowed_blocks filter.

    function weplugins_modify_excerpt_allowed_blocks_defaults($allowed_blocks) { 
        // Update the $allowed_blocks variable.
        return $allowed_blocks; 
    }
    // Add the filter
    add_filter("excerpt_allowed_blocks", "weplugins_modify_excerpt_allowed_blocks_defaults", 10, 1);
    

Example 2: Applying the Filter

Here’s how you apply the filter directly, which is essential for customizing the list of allowed blocks for excerpts.

    apply_filters('excerpt_allowed_blocks', (string[]) $allowed_blocks);
    

Example 3: Removing the Filter

If you need to remove a filter callback, you can do so with the following code. Ensure to use the same callback function name, priority, and number of arguments.

    remove_filter("excerpt_allowed_blocks", "weplugins_modify_excerpt_allowed_blocks_defaults", 10, 1);
    

Parameters:

  • $allowed_blocks: (string[]) The list of names of allowed blocks.

Access Premium WordPress Plugins

Contact Us

If you need any customization or help using this hook, feel free to contact us. Our team at WePlugins is always ready to assist you with your WordPress development needs.

 

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

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.