11

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.

3 Answers 3

18

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.

6
  • 3
    if ( is_admin() ) add_action(). Do not populate the filter array with values you never need.
    – fuxia
    Jul 17, 2013 at 18:51
  • I often check(ed) inside the callback - after all, I figured, the statement(s) will never be interpreted, if the condition isn't met. The overhead in the filter array I did not consider. Ever. Jul 17, 2013 at 18:56
  • 4
    ! is_admin() reports true though on wp-admin and wp-login.php, so it's not a check for frontend Aug 1, 2015 at 8:17
  • 2
    Keep in mind that is_admin won't check the cron and ajax calls. So if you really need frontend only this might not be sufficient.
    – janw
    Nov 12, 2016 at 15:56
  • 2
    To followup as @janw mentioned, a true frontend check would look like this ( ! is_admin() || wp_doing_ajax() ) && ! wp_doing_cron()
    – WPExplorer
    May 5, 2020 at 6:38
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.

0

Unfortunately is_admin() function not work properly if you need to check in Widget::widget() is the code retrived on front-end or on admin page.

Because of that there is a problem to distinguish the view of what the widget displays on the blog and what on the widget.php subpage of the admin panel. On dashboard widgets.php page, widget preview is an output from <iframe> HTML tag, so is_admin() is not a good solution in such case.

Because there is no is_front_end() function in Wordpress, the interesting solution is to create a function which check is get_header() and get_footer() functions has been fired during the current request. Simple function.

<?php 

    function is_front_end() {
        if ( did_action( 'get_header' ) || did_action( 'get_footer' ) ) 
            return true;
    }

?>

Inspiration thanks to @TheDeadMedic answer:

https://wordpress.stackexchange.com/a/228236/84343

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.