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