WordPress template_include Filter: Examples [Guide]

Browser window with a landing.php template chip and swap arrows, titled WordPress template_include

Your theme has a template for each page type, but one landing page needs a different layout. Editing the theme’s files would change every page that uses them, which is not what you want.

That is what the template_include filter is for. It lets you replace the template file WordPress has chosen for a request with one of your own. This guide explains how it works, shows two practical examples, and covers how it differs from template_redirect.

What the template_include Filter Does

WordPress uses the template hierarchy to choose a PHP file for each front-end request. Just before it loads that file, it passes the file path through the template_include filter. Your callback receives the path and returns the file to load. When your condition does not match, return the original path and WordPress carries on as normal.

template_include vs template_redirect

Both hooks run during a front-end request, but they do different jobs. template_redirect runs first, so you can redirect or stop the request before any template is chosen. template_include runs after it and only decides which file loads.

template_includetemplate_redirect
TypeFilterAction
Main jobChooses which template file loadsRuns code before any template loads
Return valueMust return a file pathReturns nothing
Stops the requestNoOnly if you call exit
OrderRuns after template_redirectRuns first

Use template_redirect when the visitor should go somewhere else. Use template_include when the page should still render, just with a different file. For hook basics, see our add_action guide.

Tip: Always return $template when your condition does not match. Returning an empty value leaves WordPress with no file to load.

Practical Examples

Load a Custom Template for One Page

This example loads a file from the child theme for the page with the landing slug. The file_exists() check means a missing file falls back to the default template instead of breaking the page.

<?php

add_filter( 'template_include', 'my_landing_template' );

function my_landing_template( $template ) {
    if ( is_page( 'landing' ) ) {
        $custom = get_stylesheet_directory() . '/templates/landing.php';

        if ( file_exists( $custom ) ) {
            return $custom;
        }
    }

    return $template;
}

Use a Template for a Custom Post Type

The same pattern works for custom post types. This one loads a wider single-view file for event posts only, and leaves every other post type alone.

<?php

add_filter( 'template_include', 'my_event_template' );

function my_event_template( $template ) {
    if ( is_singular( 'event' ) ) {
        $custom = get_stylesheet_directory() . '/single-event-wide.php';

        if ( file_exists( $custom ) ) {
            return $custom;
        }
    }

    return $template;
}

FAQs About template_include

Is template_include the same as template_redirect?

No. template_include is a filter that picks a template file. template_redirect is an action that can send the visitor elsewhere, or stop the request, before a template loads.

Does template_include run on every page?

It runs on front-end requests that load a template. It does not run in the admin area.

Should I use template_include or a page template?

For a one-off change, a page template chosen in the editor is simpler. Use template_include when the choice depends on logic, such as the post type, a taxonomy, or user state.

Why does my template_include filter not run?

Check that the callback returns a file path, not null or a boolean. Also confirm the code loads on the front end, for example in functions.php or a plugin. Code inside a template file runs too late to affect the choice.

Conclusion

template_include lets you change the template file for a request without editing the theme. Keep a fallback in every callback, check that your file exists, and switch to template_redirect when the visitor should go somewhere else. For custom URLs that do not map to a post, see our guide to custom routes in WordPress.

Need a custom template setup or a theme built around your site’s logic? Hire an expert WordPress developer, or reach out to me directly.

About The Author

Picture of Andriy Haydash

Andriy Haydash

Andriy Haydash is a WordPress Expert who helps people build and launch successful WordPress membership and e-learning websites.

Note: Not all of the articles are written directly by me.
Affiliate Disclaimer: Some links in the post may be my affiliate links

Kinsta - Managed Hosting for WordPress

Before YOU Leave...
Join My Newsletter

Get practical tips & tricks on how to start, grow and market your course/membership site.