Filter Hook since 1.5.0
template_include
Filters the final template file path WordPress is about to load
Description
template_include is the very last stop in the template hierarchy — return an absolute path to a PHP file and WordPress loads exactly that, bypassing the rest of the hierarchy.
When it runs
At the very end of template-loader.php's template hierarchy resolution, right before include().
Signature
apply_filters( 'template_include', string $template );Parameters
$templatestring — Absolute path to the resolved template file.
Examples
Basic
add_filter( 'template_include', function( $template ) {
if ( is_singular( 'book' ) ) {
$custom = plugin_dir_path( __FILE__ ) . 'templates/single-book.php';
if ( file_exists( $custom ) ) {
return $custom;
}
}
return $template;
} );Serve a template shipped with a plugin for a custom post type, if the theme doesn't provide one.
Common Use Cases
- Ship custom templates from a plugin
- Override a theme's template for a specific condition
- Serve a completely custom page (landing pages, builders)
Common mistakes
- Returning a template path that doesn't exist or isn't checked with file_exists() — causes a fatal include error
Related hooks
FAQ
template_include vs single_template — which should I use?
single_template (and its siblings like page_template) only affect one specific query type; template_include is the catch-all that runs regardless of query type.
Source: wp-includes/template-loader.php