Should I use is_admin() inside ‘admin_init’ hook callback

I’m adding an action witht he following line:

add_action( 'admin_init', 'fb_init_scripts');

Read More

And my function looks like this:

  function fb_init_scripts() {
    //Only use these scripts in admin interface
    if (is_admin() ) {
        wp_enqueue_script('jquery-ui','http://ajax.googleapis.com/ajax/libs/jqueryui/1.7/jquery-ui.min.js',array('jquery'));
    }
  }

In this instance, is it pointless to use is_admin() to controll if I’m in admin panel, since I’m running this function in admin_init ?

Related posts

Leave a Reply

2 comments

  1. Yep, pointless.

    admin_init only fires in admin area (and couple more files related to AJAX functionality) so additional is_admin() check is not necessary.

    It often comes up in examples for hooks that fire both on front-end and admin area.

  2. As Rarst said it’s redundant. The ajax interface happens to be in the admin area for reasons related to authentication, and WP considers it to be part of the admin area — including when it technically isn’t.

    A good “trick”, that said, is to wrap applicable add_action() calls in an if block:

    if ( is_admin() ) {
      add_action('admin_init', 'foo');
    }
    

    Doing so avoids polluting the $wp_filter global on the front end.

    Better yet, create a separate file with admin-related code, and only include it in the admin area (or when needed, using the 'load-$pagehook' action). You can see an example of how this is done here:

    http://wordpress.org/extend/plugins/sem-external-links/