- What template_redirect Does
- Example 1: Redirect Logged-Out Visitors
- Example 2: Redirect an Old Page
- Example 3: Redirect Empty Category Archives
- template_redirect vs template_include
- FAQs About WordPress template_redirect
- Does template_redirect Run in the WordPress Admin?
- Should I Use wp_redirect or wp_safe_redirect?
- What Priority Should My Callback Use?
- Can I Use template_redirect to Show a Custom Template?
- Conclusion
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 afterwp_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.
| Aspect | template_redirect | template_include |
|---|---|---|
| Hook type | Action | Filter |
| Purpose | Redirect or stop the request early | Replace the template file WordPress loads |
| Return value | None needed | The template file path |
| Typical use | Redirects, access checks, early exits | Custom 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.
