Action Hook since 1.5.0

template_redirect

Fires right before WordPress decides which template to load

Description

template_redirect is the standard place to short-circuit a request — custom redirects, maintenance mode, or blocking access — after the query is known but before any template file loads.

When it runs

Front-end only, after the query but before wp-includes/template-loader.php includes a template file.

Signature

do_action( 'template_redirect' );

Examples

Basic

add_action( 'template_redirect', function() {
    if ( is_page( 'old-page' ) ) {
        wp_safe_redirect( home_url( '/new-page/' ), 301 );
        exit;
    }
} );

301-redirect one page to another based on conditional tags.

Common Use Cases

  • Custom redirects based on conditional tags
  • Block access / maintenance mode
  • Serve a custom response instead of a template (e.g. JSON)
  • Force login on specific pages

Common mistakes

  • Redirecting on init instead — conditional tags like is_page()/is_singular() aren't reliable that early because the main query hasn't run yet

Related hooks

FAQ

Why doesn't is_page() work in my redirect on init?

The main query hasn't executed yet on init. Use template_redirect, where the query is finalized and conditional tags are reliable.

Source: wp-includes/template-loader.php

Group wizard

One session: this hook, then each related_hooks entry. Walk it and tell us what to change.

template_redirect → pre_get_posts → template_include

functions.php — template_redirect
functions.php
PHP template_redirect · Hook 1 of 3 · Step 1 of 6

Practice the template_redirect WordPress action hook in our interactive sandbox — a VS Code-style editor with a step-by-step template_redirect tutorial, add_action exercises, and instant feedback. No local WordPress install required.