Using WordPress ‘get_default_block_template_types()’ PHP function

The get_default_block_template_types() WordPress PHP function returns a filtered list of default template types, containing their localized titles and descriptions.

Usage

$default_template_types = get_default_block_template_types();

Parameters

  • None

More information

See WordPress Developer Resources: get_default_block_template_types

Examples

Display the list of default block template types

This code displays the list of default block template types with their localized titles and descriptions.

$default_template_types = get_default_block_template_types();

foreach ($default_template_types as $template_type) {
    echo 'Type: ' . $template_type['type'] . '<br>';
    echo 'Title: ' . $template_type['title'] . '<br>';
    echo 'Description: ' . $template_type['description'] . '<br><br>';
}

Create a dropdown menu with default block template types

This code creates a dropdown menu of default block template types, displaying their localized titles.

echo '<select name="template_types">';

$default_template_types = get_default_block_template_types();
foreach ($default_template_types as $template_type) {
    echo '<option value="' . $template_type['type'] . '">' . $template_type['title'] . '</option>';
}

echo '</select>';

Filter default block template types

This code filters out the ‘cover’ block template type from the list of default block template types.

function exclude_cover_template_type($default_template_types) {
    return array_filter($default_template_types, function ($template_type) {
        return $template_type['type'] !== 'cover';
    });
}
add_filter('default_block_template_types', 'exclude_cover_template_type');

Modify the title of a default block template type

This code modifies the title of the ‘cover’ block template type to ‘Custom Cover’.

function modify_cover_template_title($default_template_types) {
    foreach ($default_template_types as &$template_type) {
        if ($template_type['type'] === 'cover') {
            $template_type['title'] = 'Custom Cover';
        }
    }
    return $default_template_types;
}
add_filter('default_block_template_types', 'modify_cover_template_title');

Add a new custom block template type

This code adds a new custom block template type to the list of default block template types.

function add_custom_block_template_type($default_template_types) {
    $custom_template_type = array(
        'type' => 'custom',
        'title' => 'Custom Template',
        'description' => 'This is a custom block template type.',
    );
    array_push($default_template_types, $custom_template_type);
    return $default_template_types;
}
add_filter('default_block_template_types', 'add_custom_block_template_type');