Intro
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.
Examples
Performing a redirect
Following example will redirect all non-authenticated users to a custom ‘signup’ page when trying to visit the ‘goodies’ page.
function my_page_template_redirect()
{
if( is_page( ‘goodies’ ) && ! is_user_logged_in() )
{
wp_redirect( home_url( ‘/signup/’ ) );
die;
}
}
add_action( ‘template_redirect’, ‘my_page_template_redirect’ );
Don’t forget to exit (or die) after a wp_redirect().
Loading a different template
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.
Source Files
This action is applied in wp-includes/template-loader.php
There is definately a lot to find out about this topic. I like all of the points you made.
Thanks for sharing your thoughts on Bài thuốc chữa bệnh đau lưng.
Regards