Why is it wrong to use admin_print_scripts-{hook} to enqueue a script (.js) file?

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.

Leave a Comment