I have several posts that contains some specific dynamic content that expects certain parameters that are stored in post's custom fields. The site uses self-developed plugin that uses global $post->ID to get the current post ID.

At the plugin init hook the global $post variable is not yet populated, so it's too early to retrieve the parameters stored in the custom fields. So at that stage I can just retrieve some plugin config parameters, nothing post-related.

What hook can I add to my plugin code that would be triggered when the global $post variable is available? I'd then populate a global array variable that is later used by plugin's shortcodes.

Another thing to take into account here is that there can be run-time errors when processing the post-specific data. In case of error, I'd like to redirect to an error page. So this means that I need to know the post ID before any content has started streaming to user's browser, otherwise the plugin can not perform http redirect to the error page.


The earliest action you can use is 'wp'. You can read more about it here: Action WP.

  • While wp is the earliest action, one could argue that it’s not appropriate for code that may result in redirection. – Milo Jan 24 '18 at 21:48
  • It depends on what the user needs the query/post. WordPress would benefit from few more actions to handle everything done between 'wp' and 'template_redirect'. – Milan Petrovic Jan 24 '18 at 22:03

The template_redirect action could be appropriate for this.

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.

You can see where this occurs in the request process in the Action Reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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