I’m trying to build a feature into my theme that relies on doing things before the headers are sent. So naturally I hooked into the Init hook like so:
add_action('init', 'my_function');
But the problem is that I only want my_function to run if the user is not looking at the admin section or the login page.
So what hook can I use that is front-end only but is run before the headers are sent. Looking at the API reference, there doesn’t appear to be any, and obviously conditionals don’t work that early in the runtime.
So other than searching the URL for /wp-admin/ and /wp-login/ (which seems clunky to me) I can’t figure it out.
Wrap up your action’s hooks and function in an
if(!is_admin()){}
Like so :
Here’s how I do it. Using the
wp
action hook is late enough to provide access to the query and therefore the conditionals, but still happens before the template is set up.Edit: I misunderstood what you meant by headers (I was thinking about
wp_head
… too much theme coding lately!). I’m now assuming you’re trying to beat thesend_headers
action:It’s not super-elegant, but at least it’s concise. And it seems likely it will continue to work, which is always good news.
here is some fine and cool solution, hope you all will like it.
that is all , use and see the magic.
My solution for only front-end (not admin) is also to check the referer, because there are some ajax calls which direct to non
/wp-admin
url sequence, andis_admin()
is not fixed. It seems he is looking right at this piece of URL.