WordPress template_redirect Hook: 3 Examples [Guide]

Browser window with a redirect arrow and the title WordPress template_redirect

Ever tried to block a page for logged-out visitors, only to see WordPress render it anyway? The template_redirect hook is where that check belongs, because it runs before WordPress chooses a template and draws the page.

That’s what the template_redirect hook is for: redirects, access checks, and early exits. This guide covers how it works, three practical examples, and how it differs from template_include.

What template_redirect Does

WordPress handles a front-end request in stages. It runs the main query, fires the template_redirect action, then selects and renders a template file. Callbacks on this hook run after the query is known but before anything is rendered.

Attach a callback with add_action(). If that function is new to you, read the add_action function guide first. The pattern looks like this:

<?php

add_action( 'template_redirect', 'my_callback_name' );

function my_callback_name() {
    // Check the request, then redirect and exit if needed.
}

Example 1: Redirect Logged-Out Visitors

Use case: a members-only page should send guests to the login form, then return them to the page after they sign in.

<?php

add_action( 'template_redirect', 'my_redirect_logged_out_members' );

function my_redirect_logged_out_members() {
    if ( is_page( 'members' ) && ! is_user_logged_in() ) {
        wp_safe_redirect( wp_login_url( get_permalink() ) );
        exit;
    }
}

The wp_login_url() call passes the current page as the redirect target, so the visitor lands back on the members page after logging in.

Tip: Always call exit; right after wp_safe_redirect(). Without it, the rest of the request still runs and can output content after the redirect has been sent.

Example 2: Redirect an Old Page

Use case: you renamed a page and want visitors and search engines sent to the new address with a permanent 301 status.

<?php

add_action( 'template_redirect', 'my_redirect_old_services' );

function my_redirect_old_services() {
    if ( is_page( 'old-services' ) ) {
        wp_safe_redirect( home_url( '/services/' ), 301 );
        exit;
    }
}

Use 301 for permanent moves and 302 for temporary ones. The status code is the second argument of wp_safe_redirect().

Example 3: Redirect Empty Category Archives

Use case: a category with no published posts shows an empty listing. Send those visitors to the homepage instead.

<?php

add_action( 'template_redirect', 'my_redirect_empty_category' );

function my_redirect_empty_category() {
    global $wp_query;

    if ( is_category() && 0 === $wp_query->post_count ) {
        wp_safe_redirect( home_url( '/' ) );
        exit;
    }
}

The main query has already run at this point, so $wp_query->post_count is available to check.

template_redirect vs template_include

Both hooks run while WordPress works out which template to use, but they do different jobs.

Aspecttemplate_redirecttemplate_include
Hook typeActionFilter
PurposeRedirect or stop the request earlyReplace the template file WordPress loads
Return valueNone neededThe template file path
Typical useRedirects, access checks, early exitsCustom page templates for matching URLs

Use template_redirect to send the visitor somewhere else. Use template_include when you want a different template file for the same URL. It is a filter, so it must return a file path, and WordPress then renders that file. For mapping new URLs to templates, see creating custom routes in WordPress.

FAQs About WordPress template_redirect

Does template_redirect Run in the WordPress Admin?

No. The hook fires only on front-end requests, because the template loader runs only there.

Should I Use wp_redirect or wp_safe_redirect?

Use wp_safe_redirect() unless you must send visitors to another domain. By default it only allows your own host. To allow another domain, add it with the allowed_redirect_hosts filter.

What Priority Should My Callback Use?

The default priority of 10 works for most cases. Use a lower number if your redirect must run before other plugins’ callbacks on the same hook.

Can I Use template_redirect to Show a Custom Template?

Not cleanly. Use template_include for that, because it returns a template file path instead of ending the request.

Conclusion

Use template_redirect for redirects and early exits, and template_include for swapping template files. Start with the example closest to your case, and test it on a staging copy first.

Need custom redirect rules, access control, or template work built correctly? See my custom WordPress development services, or reach out to me directly to discuss your project.

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.