Using WordPress ‘get_dynamic_block_names()’ PHP function

The get_dynamic_block_names() WordPress PHP function returns an array of the names of all registered dynamic block types.

Usage

$block_names = get_dynamic_block_names();
print_r($block_names);

Output:

Array
(
    [0] => core/block
    [1] => core/latest-comments
    ...
)

Parameters

  • None

More information

See WordPress Developer Resources: get_dynamic_block_names()

Examples

Display a list of dynamic block names

Retrieve and display the names of all dynamic blocks.

$block_names = get_dynamic_block_names();
echo implode(', ', $block_names);

Check if a specific block is a dynamic block

Check if ‘core/paragraph’ is a dynamic block and display a message accordingly.

$block_names = get_dynamic_block_names();
$block_to_check = 'core/paragraph';

if (in_array($block_to_check, $block_names)) {
    echo "'{$block_to_check}' is a dynamic block.";
} else {
    echo "'{$block_to_check}' is not a dynamic block.";
}

Count the number of dynamic blocks

Get the total number of dynamic blocks and display the count.

$block_names = get_dynamic_block_names();
$block_count = count($block_names);
echo "There are {$block_count} dynamic blocks.";

Display dynamic blocks starting with ‘core’

Show all dynamic block names that start with the ‘core’ namespace.

$block_names = get_dynamic_block_names();
$core_blocks = array_filter($block_names, function($block_name) {
    return strpos($block_name, 'core/') === 0;
});
echo implode(', ', $core_blocks);

Remove a specific dynamic block

Remove a specific dynamic block (‘core/latest-posts’) from the list of dynamic block names.

$block_names = get_dynamic_block_names();
$block_to_remove = 'core/latest-posts';
$block_names = array_diff($block_names, [$block_to_remove]);
print_r($block_names);