I am not clear when/where I will use wp_enqueue_script()
or wp_register_script()
问题:
回答1:
In simple words;
wp_enqueue_script
means, Add in a queue to register the file
wp_register_script
means, Register the file immediately
wp_register_script
registers a script to be enqueued later using thewp_enqueue_script()
function.
wp_enqueue_script()
registers the script if $src provided (does NOT overwrite), and enqueues it. source: Developer.wordpress
Here is brief Answer on WordpressDevelopment
wp_register_script( $handle, $src, $deps, $ver, $in_footer );
Scripts that have been pre-registered using wp_register_script() do not need to be manually enqueued using wp_enqueue_script() if they are listed as a dependency of another script that is enqueued. WordPress will automatically include the registered script before it includes the enqueued script that lists the registered script’s handle as a dependency.
wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );
wp_enqueue_script() links a script file to the generated page at the right time according to the script dependencies, if the script has not been already included and if all the dependencies have been registered. You could either link a script with a handle previously registered using the wp_register_script() function, or provide this function with all the parameters necessary to link a script.
回答2:
Base on wordpress.org:
Scripts that have been pre-registered using wp_register_script() do not need to be manually enqueued using wp_enqueue_script() if they are listed as a dependency of another script that is enqueued.
WordPress will automatically include the registered script before it includes the enqueued script that lists the registered script’s handle as a dependency.
Notes:
- The function should be called using the wp_enqueue_scripts action hook if you want to call it on the front-end of the site. To call it on the administration screens, use the admin_enqueue_scripts action hook. For the login screen, use the login_enqueue_scripts action hook. Calling it outside of an action hook can often lead to unexpected results and should be avoided.
- If attempt to register or enqueue an already registered handle with different parameters, the new parameters will be ignored. Instead, use wp_deregister_script() and register the script again with the new parameters.
- jQuery UI Effects is not included with the jquery-ui-core handle
回答3:
To my understanding the wp_register_scripts
and the wp_register_scripts
are function that registers your script(js) and style (css) immediately but does not directly load them in your Wordpress pages.
There is a kind of switch()
statement that works with these functions making them to load your "scripts" only on pages that needs them.
It is not necessary to register all your scripts but this can add some logic to your code and surely, it makes you a better developer; with a sense of clean code writing.
A good example is when enqueuing a custom stylesheet for your admin area, or a custom js script (e.g to work with the Wordpress Media Uploader). In these two scenarios you should use the wp_register_style
and the wp_register_script
respectively. Furthermore, in both cases you call your function at the admin_enqueue_scripts
so a good rule of thumb is when you are hooking into the admin_enqueue_scripts
be sure to register your script.