Using WordPress ‘get_block_template’ PHP filter

The get_block_template WordPress PHP filter allows you to modify the block template object after it’s been fetched.

Usage

add_filter( 'get_block_template', 'your_custom_function', 10, 3 );
function your_custom_function( $block_template, $id, $template_type ) {
    // your custom code here

    return $block_template;
}

Parameters

  • $block_template (WP_Block_Template|null) – The found block template, or null if there isn’t one.
  • $id (string) – Template unique identifier (example: theme_slug//template_slug).
  • $template_type (array) – Template type: ‘wp_template’ or ‘wp_template_part’.

More information

See WordPress Developer Resources: get_block_template

Examples

Change block template content

Modify the block template content before it’s rendered.

add_filter( 'get_block_template', 'change_block_template_content', 10, 3 );
function change_block_template_content( $block_template, $id, $template_type ) {
    // Modify content if the template type is 'wp_template'
    if ( $template_type === 'wp_template' ) {
        $block_template->content = str_replace( 'old_text', 'new_text', $block_template->content );
    }
    return $block_template;
}

Add a custom class to block template

Add a custom class to the block template’s HTML.

add_filter( 'get_block_template', 'add_custom_class_to_block_template', 10, 3 );
function add_custom_class_to_block_template( $block_template, $id, $template_type ) {
    // Add custom class to the block template
    $block_template->html = str_replace( 'class="', 'class="custom-class ', $block_template->html );
    return $block_template;
}

Exclude specific block template by ID

Prevent a specific block template from being used.

add_filter( 'get_block_template', 'exclude_specific_block_template', 10, 3 );
function exclude_specific_block_template( $block_template, $id, $template_type ) {
    // Exclude the block template with specific ID
    if ( $id === 'theme_slug//template_slug' ) {
        return null;
    }
    return $block_template;
}

Modify block template based on user role

Change the block template depending on the user’s role.

add_filter( 'get_block_template', 'modify_block_template_for_user_role', 10, 3 );
function modify_block_template_for_user_role( $block_template, $id, $template_type ) {
    // Get the current user
    $user = wp_get_current_user();

    // If user has the 'subscriber' role, modify the block template
    if ( in_array( 'subscriber', $user->roles ) ) {
        $block_template->content = str_replace( 'old_text', 'subscriber_text', $block_template->content );
    }
    return $block_template;
}

Change block template for a specific post type

Modify the block template for a specific post type.

add_filter( 'get_block_template', 'change_block_template_for_post_type', 10, 3 );
function change_block_template_for_post_type( $block_template, $id, $template_type ) {
    // Get the current post type
    $post_type = get_post_type();

    // If the post type is 'custom_post_type', modify the block template
    if ( $post_type === 'custom_post_type' ) {
        $block_template->content = str_replace( 'old_text', 'custom_post_type_text', $block_template->content );
    }
    return $block_template;
}