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
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>
Use case: On all admin pages
Hook: admin_print_scripts
Use case: On all front end pages
Hook: wp_enqueue_scripts
And the callback:
Note: Use the same for enqueueing styles too (wp_enqueue_style)!
Edit: I checked the codex for
admin_print_scripts
, they now suggest to useadmin_enqueue_scripts
instead. I ran a search through version 3.4.1 core files, and found they useadmin_print_scripts-<hook>
internally! So you can use it too!It works flawlessly!