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_register_script('ae-admin', get_bloginfo('template_directory') . '/js/admin.js', array('jquery', 'jquery-validate'));
    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');

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?

4 Answers 4


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)

  • 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. Commented Jun 20, 2011 at 12:35
  • @Chris_O, I suppose something like pastie.org/2099186, it still does not work ...
    – JM at Work
    Commented Jun 21, 2011 at 2:09
  • @Chip Bennett, I am making a theme, the JS are for a theme options page
    – JM at Work
    Commented Jun 21, 2011 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
    Commented Jun 21, 2011 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). Commented Jun 21, 2011 at 2:19

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' );
  • Holly Javascript Batman! Good Catch :)
    – mfields
    Commented Jun 21, 2011 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. Commented Jun 21, 2011 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
    Commented Jun 21, 2011 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
    Commented Jun 23, 2011 at 1:46

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() ?>
  • He's enqueuing the scripts on the admin side not the front end.
    – Chris_O
    Commented Jun 20, 2011 at 11:40

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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