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.
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.
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.
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.