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?
Use the
admin_enqueue_scripts
hook instead ofadmin_init
Note: you should use hooks that target admin pages as specifically as possible. e.g.:
admin_print_scripts-{plugin-page}
hookadmin_print_scripts-{theme-page}
hook (where{theme-page}
is whatever string you use in theadd_theme_page()
call)admin_print_scripts-edit.php
hook,For Custom Post Types, inside your function, do something like the following:
(h/t t31os)
I just noticed that your
add_action()
call is incorrect. You have to pass it a callback, not the function definition.You have:
Instead, you should have:
Make sure your header.php (or another function in the header) runs the wp_head action hook. The code should look like this:
Actually I just had a typo
But thanks @Chris_O, & @Chip Bennett for their great answers