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.

According to the Codex, it should only be used for inline script tag(s) in the head. I've been using add_action('admin_print_scripts-{my-hook}' for injecting .js and add_action('admin_print_styles-{my-hook}' for .css without any problems. I use wp_register_script() & wp_enqueue_script() (and the corresponding *_style() ) to enqueue them.

A "view source" shows

<link rel='stylesheet' href=[...] />

and

<script type='text/javascript' src=[...] ></script>

being inserted into the <head> correctly.

Does anyone know any reason not to use them this way, other than a poorly-written Codex description that says not to?

share|improve this question
add comment

1 Answer 1

The Problem if you just output the scripts (thats what the admin_print_scripts is for) is that you may end up having the same Javascriptfile included multiple times, which can lead to errors on your page.

If you use the wp_enqueue_script() WordPress checks if the requested Javascriptfile is already in the Queue, and what dependencies it relies on, so that they are included in the right order.

The admin_print_scripts should only be used to output the data.

There are a few ways to make the same things work (you could also hardcode the include into your files - but do not do that) - but you should always do it in the way it is suggested by WordPress - to ensure future compatibility and everything else WordPress offers.

share|improve this answer
    
Thanks, fischi. As noted in my Question, I already do use wp_register_*() as well as wp_enqueue_*(). I use the hooks admin_print_scripts-{my-hook} and admin_print_styles-{my-hook} to call them. I've seen these hooks used in more than one tutorial (can't remember where/too lazy to search), so I was wondering if anyone knows why the Codex says not to use them. Again, that particular part of the Codex is poorly written, so it calls into question its canonical-ness. –  akTed Jan 16 '13 at 10:59
1  
@AKTed the reason you may want to use another hook is that you could enqueue your Javascript earlier. Does not really make a difference in the outcome, but the later you call an enqueue_script, the less there are possibilities to dequeue the script, if needed. Always depends on what you want to achieve, but enqueueing AFTER admin_print_scripts would be pointless ;) –  fischi Jan 16 '13 at 11:25
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.