What is the condition to check if we are in admin or 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.

Related posts

2 comments

  1. Take a look at the is_admin() conditional tag:

    function wpse106895_dummy_func() {
        if ( ! is_admin() ) {
            // do your thing
        }
    }
    add_action( 'some-hook', 'wpse106895_dummy_func' );
    

    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:

    function wpse106895_dummy_func() {
        // do your thing
    }
    if ( ! is_admin() ) add_action( 'some-hook', 'wpse106895_dummy_func' );
    

    will save you overhead.

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

Comments are closed.