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 am trying to enqueue/print scripts in the admin area. But they dont seem to appear.

add_action('admin_init', function() {
    add_meta_box('portfolio-meta', 'Details', 'portfolio_metabox_details', 'portfolio');

    wp_register_script('jqeury-validate', 'http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8.1/jquery.validate.min.js');
    wp_enqueue_script('jquery-validate');
    wp_register_script('ae-admin', get_bloginfo('template_directory') . '/js/admin.js', array('jquery', 'jquery-validate'));
    wp_enqueue_script('ae-admin');
    wp_localize_script('ae-admin', 'WpAjax', array(
            'AjaxUrl' => admin_url('admin-ajax.php')
    ));
    wp_register_style('ae-validate', get_bloginfo('template_directory') . '/css/validate.css');
    wp_enqueue_style('ae-validate');
});

But my script (admin.js) does not seem to get printed. I even tried to put those in 'init' instead of 'admin_init' still I dont see my scripts ... why is that? How can I debug?

share|improve this question
add comment

4 Answers 4

up vote 8 down vote accepted

Use the admin_enqueue_scripts hook instead of admin_init

Note: you should use hooks that target admin pages as specifically as possible. e.g.:

  • Plugins: Use the admin_print_scripts-{plugin-page} hook
  • Themes: Use the admin_print_scripts-{theme-page} hook (where {theme-page} is whatever string you use in the add_theme_page() call)
  • Custom Post-Type Edit Page: Use the admin_print_scripts-edit.php hook,

For Custom Post Types, inside your function, do something like the following:

global $typenow;
if( 'portfolio' == $typenow ) {
    // wp_enqueue_script() calls go here
}

(h/t t31os)

share|improve this answer
1  
Note also: if these scripts are for a particular Plugin, then you should only enqueue such scripts on admin pages specific to your Plugin, using the admin_print_scripts-{plugin-page} hook. –  Chip Bennett Jun 20 '11 at 12:35
    
@Chris_O, I suppose something like pastie.org/2099186, it still does not work ... –  JM at Work Jun 21 '11 at 2:09
    
@Chip Bennett, I am making a theme, the JS are for a theme options page –  JM at Work Jun 21 '11 at 2:09
    
Ok, I just put pastie.org/2099209 in functions.php directly instead of in a hook and it works. But I think its wrong? Also whats wrong with it? Also why does admin_enqueue_scripts/styles not working .... –  JM at Work Jun 21 '11 at 2:14
    
@JM at Work: the principle applies to Themes as well as to Plugins. For Themes, use the admin_print_scripts-{theme-page} hook (where {theme-page} is whatever string you use in the add_theme_page() call). –  Chip Bennett Jun 21 '11 at 2:19
show 8 more comments

Actually I just had a typo

wp_register_script('jqeury-validate', 'http://ajax.aspnetcdn.com/ajax/jquery.validate
                      ^

But thanks @Chris_O, & @Chip Bennett for their great answers

share|improve this answer
add comment

I just noticed that your add_action() call is incorrect. You have to pass it a callback, not the function definition.

You have:

add_action('admin_init', function() {
    // function definition here
});

Instead, you should have:

function mytheme_enqueue_admin_scripts() {
     // register/enqueue code goes here
}
add_action( 'hook_name', 'mytheme_enqueue_admin_scripts' );
share|improve this answer
    
Holly Javascript Batman! Good Catch :) –  mfields Jun 21 '11 at 21:45
    
I suppose it's possible that an anonymous function will work there, but I've never seen it used in the wild. –  Chip Bennett Jun 21 '11 at 22:09
    
Totally, Looks like it should work in PHP >= 5.3. As far as I know this goes against best practices though. I remember Nacin mentions something along these lines at WordCamp Seattle using create_function() as an example and I've since read about it not-so-good elsewhere. One reason given is that there is no way to un-hook the function. –  mfields Jun 21 '11 at 22:19
    
I was using an anonymous function as I know I will be using >= PHP 5.3 and since I am only using the function once. I'll try it without anonymous functions sometime –  JM at Work Jun 23 '11 at 1:46
add comment

Make sure your header.php (or another function in the header) runs the wp_head action hook. The code should look like this:

<?php wp_head() ?>
share|improve this answer
    
He's enqueuing the scripts on the admin side not the front end. –  Chris_O Jun 20 '11 at 11:40
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.