I'm developing a plugin and need to know on what page user is and then add specific hooks and filters for that page.
And my problem is that is_page() and the_ID() doesn't work outside of wp, wp_loaded, init actions, but if i use these actions then i can't initialize hooks because for them to work properly they must be added before those actions.
So my question being, is there any hook from witch i can call the_ID(), get a proper result and then add another hooks for that page? Or is it possible only using PHP's $_SERVER['REQUEST_URI']?

1 Answer 1


The earliest safe hook to get post information is the template_redirect hook. All the hooks in question runs before Wordpress has setup postdata, so any post info are still unavailable at that point.

The globals like $wp_query and $post will still contain no data, that is why your efforts returns nothing.


Extra info as per comment by @TheDeadMedic

Actually wp is an earlier, still-safe hook - just make sure to use get_queried_object() instead of relying on $post global

  • 1
    Actually wp is an earlier, still-safe hook - just make sure to use get_queried_object() instead of relying on $post global. Aug 28, 2015 at 9:10
  • template_redirect is actually exactly what i needed, thank you! Also wp hook doesn't appear to work for me even with get_queried_object(), just returns NULL. But my problem is now solved thanks to you and this is what important. Aug 28, 2015 at 10:17

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.