template_redirect WordPress Action Hook

The template_redirect hook is one of the most important hooks in WordPress. It is called after the theme has been selected and before the template is loaded. This hook can be used to load a different template for a specific page or post, or to redirect the user to a different page altogether.

do_action( 'template_redirect' ) #

Fires before determining which template to load.


More Information

  • This action hook executes just before WordPress determines which template page to load. It is a good hook to use if you need to do a redirect with full knowledge of the content that has been queried.
  • Loading a different template is not a good use of this action hook. If you include another template and then use exit() (or die()), no subsequent template_redirect hooks will be run, which could break the site’s functionality. Instead, use the template_include filter hook to return the path to the new template you want to use. This will allow an alternative template to be used without interfering with the WordPress loading process.

Top ↑

Source

File: wp-includes/template-loader.php

View on Trac


Top ↑

Changelog

Changelog
VersionDescription
1.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.