Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm adding an action witht he following line:

add_action( 'admin_init', 'fb_init_scripts');

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 ?

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

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.

share|improve this answer
add comment

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/

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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