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 trying to add a java script file to my admin header and use admin-ajax.php to use ajax in my wp-admin (still learning a lot about the process). I've created a custom folder in my theme_directory/js called custom with the file I need to add to the admin header but it doesn't seem to be working. here's the code:

add_action('admin_enqueue_scripts', 'my_admin_enqueue_scripts');

function my_admin_enqueue_scripts() {
    global $current_screen;
    if ( 'page' != $current_screen->ID )
        return;
    wp_register_script('my-scripts', get_template_directory_uri() . '/js/custom/my-scripts.js' );
    wp_enqueue_script('my-scripts');
    wp_localize_script('my-scripts', 'wp_ajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
}

Despite this I'm not seeing a reference in my header to my custom js file. I've even tried add_action('admin_header', 'my_admin_enqueue_scripts'); with no avail. I only need this script file in my wp-admin.

What am I missing? Thank you!

share|improve this question
add comment

2 Answers

it's not admin_header but admin_head for add action. I finf it better to put js in admin_footer.

share|improve this answer
add comment
up vote 3 down vote accepted

Ahh ok I figured it out while researching. I needed to add the $hook parameter and pass it to my function like so:

function my_admin_enqueue_scripts($hook) {
    global $current_screen;

    if ( 'post.php' != $hook )
        return;
    wp_register_script('my-scripts', get_template_directory_uri() . '/js/custom/my-scripts.js' );
    wp_enqueue_script('my-scripts');
    wp_localize_script('my-scripts', 'wp_ajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
}

This gave me the results I was looking for. Thank you!

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.