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.

Possible Duplicate:
Where is the right place to register/enqueue scripts & styles

According to wp_enqueue_script() documentation in Codex, it seems that one should hook wp_enqueue_script() calls to the wp_enqueue_scripts action instead of wp_head. That contradicts all the tutorials I have ever read on the subject, which suggests adding to wp_head.

Which is the right way to do it?

Thanks in Advance :0

share|improve this question

marked as duplicate by Rarst Sep 25 '12 at 11:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
You are reading some poor tutorials. :) Switch the sites you follow. –  Rarst Sep 25 '12 at 11:21

1 Answer 1

I took a long time figure out the right way for this! Here's what I follow now:

Use case: In a plugin's admin page

Hook: admin_print_scripts-<page hook> OR <the php file name for your plugin>

$hook = add_menu_page(...) / add_submenu_page(...);
add_action('admin_print_scripts-'.$hook, 'my_callback');

Use case: On all admin pages

Hook: admin_print_scripts

add_action('admin_print_scripts', 'my_callback');

Use case: On all front end pages

Hook: wp_enqueue_scripts

add_action('wp_enqueue_scripts', 'my_callback');

And the callback:

function my_callback(){
    wp_enqueue_script(....);
}

Note: Use the same for enqueueing styles too (wp_enqueue_style)!

Edit: I checked the codex for admin_print_scripts, they now suggest to use admin_enqueue_scripts instead. I ran a search through version 3.4.1 core files, and found they use admin_print_scripts-<hook> internally! So you can use it too!

It works flawlessly!

share|improve this answer
    
*print* hooks are semantically for script output, not enqueue. It's not set in stone but I prefer and recommend to stick with *enqueue* hooks to enqueue for consistency. –  Rarst Sep 25 '12 at 11:23

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