The pre_get_block_file_template filter allows you to modify the block template object before the theme file discovery process starts in WordPress. You can use it to return a non-null value and bypass the default WordPress theme file discovery.
Usage
add_filter( 'pre_get_block_file_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) – Return a block template object to short-circuit the default query, or null to allow WordPress to run its normal queries.
- $id (string) – The unique identifier for the template (e.g., theme_slug//template_slug).
- $template_type (string) – The template type: ‘wp_template’ or ‘wp_template_part’.
More information
See WordPress Developer Resources: pre_get_block_file_template
Examples
Overriding a specific template
Bypass the theme file discovery for a specific template (e.g., ‘header’):
add_filter( 'pre_get_block_file_template', 'override_header_template', 10, 3 );
function override_header_template( $block_template, $id, $template_type ) {
if ( 'wp_template_part' === $template_type && 'theme_slug//header' === $id ) {
// Return your custom header block template object
return $your_custom_header_template;
}
return $block_template;
}
Bypass all template parts
Bypass the theme file discovery for all template parts:
add_filter( 'pre_get_block_file_template', 'bypass_all_template_parts', 10, 3 );
function bypass_all_template_parts( $block_template, $id, $template_type ) {
if ( 'wp_template_part' === $template_type ) {
// Return your custom block template object
return $your_custom_block_template;
}
return $block_template;
}
Bypass templates for a specific post type
Bypass the theme file discovery for templates related to a specific post type (e.g., ‘product’):
add_filter( 'pre_get_block_file_template', 'bypass_product_templates', 10, 3 );
function bypass_product_templates( $block_template, $id, $template_type ) {
if ( 'wp_template' === $template_type && false !== strpos( $id, 'product' ) ) {
// Return your custom product block template object
return $your_custom_product_template;
}
return $block_template;
}
Bypass templates with a custom condition
Bypass the theme file discovery for templates based on a custom condition:
add_filter( 'pre_get_block_file_template', 'bypass_templates_with_condition', 10, 3 );
function bypass_templates_with_condition( $block_template, $id, $template_type ) {
if ( your_custom_condition() ) {
// Return your custom block template object
return $your_custom_template;
}
return $block_template;
}
Logging template information
Log template information without modifying the block template:
add_filter( 'pre_get_block_file_template', 'log_template_information', 10, 3 );
function log_template_information( $block_template, $id, $template_type ) {
error_log( "Template ID: {$id}, Template Type: {$template_type}" );
// Return the original block template without modifying it
return $block_template;
}
These examples demonstrate various use cases for the **pre_get_block_file_template** filter. You can use it to override specific templates, bypass all template parts or templates related to a specific post type, apply a custom condition, or log template information without modifying the block template.