- Plug-ins exist which force scripts to be loaded in the footer (hard-coding would mean they couldn't do this, and may even break your plug-in).
- Hardcoding means other plug-ins cannot de-register or replace your script (they should be able to). Nor can then easily use your script while avoiding loading the file twice.
- By registering a script (
wp_register_script()) you are then free to use
wp_enqueue_script() in any widgets, shortcodes or templates. Better still, the script is only enqueued if the appropriate tempate, widget or shortcode is used: impossible if its hardcoded.
wp_enqueue_script() allows you or other plug-ins to make use of
- There are problably more reasons...
assets/js. Something that makes sense.
It usually makes sense to register and enqueue your script separately:
add_action( 'init', 'wpse125424_register_scripts' );
'my-script-handle', //unique handle for our script
plugins_url( 'js/my-script.js', __FILE__ ), //url to script
array( 'jquery' ), //list any dependencies
'1.0' //version, for cache busting
and then call
The advantage of this is clear: your scripts are registered in one, easily maintained place, and divorced form the the business of queuing the script which can take place 'on the fly' and possibly subject to some contorted logic.