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.

  • 1
    +1. For sake of completeness, between 'template_redirect' and 'template_include' there is another filter hook fired, that is variable depending on the query type. Is the "{$type}_template" e.g. in case of index.php it will be 'index_termplate'.
    – gmazzap
    Commented Jun 13, 2014 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.