Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm building a plugin and I want to add bits of javascript in the admin head but only for certain admin pages. I don't mean pages as in a WordPress page that you create yourself but rather existing admin section pages like 'Your Profile', 'Users', etc. Is there a wp function specifically for this task? I've been looking and I can only find the boolean function is_admin and action hooks but not a boolean function that just checks.

share|improve this question
Gentle nudge to remind you an answer has not been accepted. If none the answers provided answer your question sufficiently or you're struggling to understand the information provided please comment to let us know. – t31os Feb 24 '11 at 17:21

3 Answers

up vote 4 down vote accepted

The way to do this is to use the 'admin_enqueue_scripts' hook to en-queue the files you need. This hook will get passed a $hook_suffix that relates to the current page that is loaded:

function my_admin_enqueue($hook_suffix) {
    if($hook_suffix == 'appearance_page_theme-options') {
        wp_enqueue_script('my-theme-settings', get_template_directory_uri() . '/js/theme-settings.js', array('jquery'));
        wp_enqueue_style('my-theme-settings', get_template_directory_uri() . '/styles/theme-settings.css');
        ?>
        <script type="text/javascript">
        //<![CDATA[
        var template_directory = '<?php echo get_template_directory_uri() ?>';
        //]]>
        </script>
        <?php
    }
}
add_action('admin_enqueue_scripts', 'my_admin_enqueue');
share|improve this answer
Thanks for sharing your info. – racl101 Apr 20 '11 at 17:26

There is a global variable in wp-admin called $pagenow which holds name of the current page, ie edit.php, post.php, etc.

You can also check the $_GET request to narrow your location down further, for example:

global $pagenow;
if (( $pagenow == 'post.php' ) && ($_GET['post_type'] == 'page')) {

    // editing a page

    }

if ($pagenow == 'users.php') {

    // user listing page

    }

if ($pagenow == 'profile.php') {

    // editing user profile page

    }
share|improve this answer

To offer another method.

// When it's the users list, or your editting another users profile
add_action( 'admin_print_scripts-users.php', 'your_enqueue_callback' );

// When you're editting your own profile
add_action( 'admin_print_scripts-profile.php', 'your_enqueue_callback' );

function your_enqueue_callback() {
    wp_enqueue_script( .. YOUR ENQUEUE ARGS .. );
}
share|improve this answer

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.