When can you get current page ID and initialize hooks right after?

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
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.

EDIT

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

Leave a Comment