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.
If you need to remove a template_redirect from within a custom plugin, simply using remove_action( 'template_redirect', 'function_to_remove' ); won’t cut it.
As a workaround, what you can do is create a function to hook into template_redirect action earlier and then remove the redirect you are trying to remove.
// remove a template redirect from within a custom plugin.
add_action( 'template_redirect', 'remove_my_action', 5 );
function remove_my_action(){
remove_action('template_redirect', 'function_to_remove', 10 );
}
Is that correct? You cannot remove callback functions before they’ve been registered. In this example you’re calling your remove_my_action() function at a higher priority than when the function_to_remove() function is registered. It’s recommended to hook your remove_my_action() function to an action hook that fires after template_redirect. For example, you can hook it to the after_setup_theme action.
@salmanravoof The callback function in Andrew’s example has been registered on the hook already, but it hasn’t fired yet. In the above example, here’s the list of events: 1)wporg_plugin_init() fires on init action, registering the template_redirect callback (at default priority 10), 2)wporg_theme_template_redirect() fires on template_redirect with priority 5, removing the registered callback (set to fire on priority 10), and 3) action template_redirect with priority 10 is running, but because the callback has been removed, it doesn’t run. Because the plugin’s callback is set to run at priority 10 during the init action, by hooking into template_redirect at an earlier priority, the callback has been registered, and has not yet fired, so it can be removed. Hope that’s helpful.
Redirect existing pages to other pages:
Example migrated from Codex:
Following example will redirect all non-authenticated users to a custom ‘signup’ page when trying to visit the ‘goodies’ page.
Don’t forget to call exit() ( or die() ) after a wp_redirect() .
If you need to remove a template_redirect from within a custom plugin, simply using
remove_action( 'template_redirect', 'function_to_remove' );
won’t cut it.As a workaround, what you can do is create a function to hook into template_redirect action earlier and then remove the redirect you are trying to remove.
remove_my_action()
function at a higher priority than when thefunction_to_remove()
function is registered. It’s recommended to hook yourremove_my_action()
function to an action hook that fires aftertemplate_redirect
. For example, you can hook it to theafter_setup_theme
action.wporg_plugin_init()
fires oninit
action, registering thetemplate_redirect
callback (at default priority10
), 2)wporg_theme_template_redirect()
fires ontemplate_redirect
with priority5
, removing the registered callback (set to fire on priority10
), and 3) actiontemplate_redirect
with priority10
is running, but because the callback has been removed, it doesn’t run. Because the plugin’s callback is set to run at priority10
during theinit
action, by hooking intotemplate_redirect
at an earlier priority, the callback has been registered, and has not yet fired, so it can be removed. Hope that’s helpful.