Using WordPress ‘excerpt_allowed_blocks’ PHP filter

The excerpt_allowed_blocks WordPress PHP filter allows you to modify the list of blocks that can contribute to the excerpt.

Usage

add_filter('excerpt_allowed_blocks', 'my_custom_allowed_blocks');

function my_custom_allowed_blocks($allowed_blocks) {
    // your custom code here
    return $allowed_blocks;
}

Parameters

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

More information

See WordPress Developer Resources: excerpt_allowed_blocks

Examples

Allow only paragraph blocks in excerpts

In this example, we only allow paragraph blocks to be part of the excerpt.

add_filter('excerpt_allowed_blocks', 'allow_only_paragraph_blocks');

function allow_only_paragraph_blocks($allowed_blocks) {
    return array('core/paragraph');
}

Add custom block to allowed blocks

In this example, we add a custom block to the list of allowed blocks for excerpts.

add_filter('excerpt_allowed_blocks', 'add_my_custom_block_to_allowed_blocks');

function add_my_custom_block_to_allowed_blocks($allowed_blocks) {
    $allowed_blocks[] = 'my-plugin/my-custom-block';
    return $allowed_blocks;
}

Remove specific block from allowed blocks

In this example, we remove the image block from the list of allowed blocks for excerpts.

add_filter('excerpt_allowed_blocks', 'remove_image_block_from_allowed_blocks');

function remove_image_block_from_allowed_blocks($allowed_blocks) {
    $key = array_search('core/image', $allowed_blocks);
    if (false !== $key) {
        unset($allowed_blocks[$key]);
    }
    return $allowed_blocks;
}

Clear allowed blocks

In this example, we clear the list of allowed blocks, meaning no blocks will contribute to the excerpt.

add_filter('excerpt_allowed_blocks', 'clear_allowed_blocks');

function clear_allowed_blocks($allowed_blocks) {
    return array();
}

Allow all core blocks in excerpts

In this example, we allow all core blocks to be part of the excerpt.

add_filter('excerpt_allowed_blocks', 'allow_all_core_blocks');

function allow_all_core_blocks($allowed_blocks) {
    // Replace 'core/' with your namespace for custom blocks
    $allowed_blocks = array('core/');
    return $allowed_blocks;
}