Is there an action hook that fires just before a template is loaded?

I’m looking for an action hook that occurs just before a template is parsed but AFTER the query has run and the variables are present.

So if it’s going to run index.php, just before it starts reading code from line 1 of index.php, some hook would be super useful.

Anyone know one?

1 Answer
1

Looking at the Action Reference in the Codex, the first action listed after the wp action that sets up the WP object is template_redirect, which is indeed fired after all queried data is available, and before a template is selected. You can use the WordPress conditionals to determine if your intended template file will be loaded.

There is also the template_include filter which is executed after a template has been selected but before it is loaded, and gets passed the selected template file as an argument. But you should note that as this is a filter hook, it’s intended purpose is to replace a selected template rather than adding or removing functionality.

Alternately, if you are using your own theme, you could call do_action( 'my_custom_action' ); at the top of your template file and simply add hooks to the action to execute custom functionality.

Leave a Comment