What is the condition to check if we are in admin or frontend?
I want to add_action not in backend, but only in frontend.
What is the condition to check if we are in admin or frontend?
I want to add_action not in backend, but only in frontend.
Comments are closed.
Take a look at the
is_admin()
conditional tag:is_admin()
returns true, if the URL being accessed is in the dashboard / wp-admin. Hence it’s negation (via the not operator) is true when in the frontend.Update, see comments below:
will save you overhead.
Backend and frontend on modern web is more of a state of mind than actual distinct url pattern, or code. Whether a specific page fulls into being a backend or frontend might change based on the kind of user is accessing the page.
In the context of wordpress the question might be rephrased to “I want to add action when the theme is generating HTML” (which, might or not cover all front end generation in your specific case), and for this, the best hook to use is
template_redirect
which is fired just before wordpress goes into deciding which template of the theme to use to generate the HTML.