[wp-hackers] wp_register_script() vs. wp_enqueue_script()?

Mike Schinkel mikeschinkel at newclarity.net
Thu Jan 22 02:10:33 GMT 2009

Chris Jean <gaarai at gaarai.com> wrote:
> Technically, you never have to register anything if you don't 
> want to. The register functions are there and can be valuable 
> if you want a central location that defines scripts and styles
> that will be used in your plugin/theme. You can then simply use 
> the enqueue functions while referring to just the handle in 
> order to enqueue the script or style for inclusion in the head.
> So, the main purpose of the register functions is to allow you 
> to simplify your code by removing the need to duplicate code 
> if you enqueue the same script or style in more than one 
> section of code. The benefits of this are many and probably 
> don't need to be listed here.

That's very clear. Thanks! 

"Austin Matzko" <if.website at gmail.com> wrote:
> wp_register_script is particularly useful when you have 
> several scripts dependent on each other.  So when you 
> initialize your plugin,you register each of the scripts 
> with their dependencies using wp_register_script. Then 
> later at the point you actually need one of the scripts 
> to be printed, you call wp_enqueue_script using the 
> handle of that particular script, and WordPress handles 
> the dependencies for you.

So does wp_enqueue_script() explicitly NOT establish a name for managing dependencies in the same way that wp_register_script() does?  If yes, why do you have to give the script a name in wp_enqueue_script()?

But anyway, it brings up another question I know from the wp_enqueue_script() docs that WP core registers several standard scripts like "jquery" and "scriptaculous." However, it doesn't (and couldn't) register many other well-known scripts people might need and that might results in them being duplicated by different plugins. One very common one I need that is not in core is "swfobject" and I know other plugins include it. For example, we might end up with the following because of two different plugins using their own local copy of the same script: 

   // In HTML generated by WordPress install with both FOO and BAR plugins
   <script type="text/javascript" src="http://example.com/wp-content/plugins/FOO/js/swfobject.js"></script>
   <script type="text/javascript" src="http://example.com/wp-content/plugins/BAR/js/swfobject.js"></script>

Has there been any thought to creating a set of "standards" for well-known scripts such that, if used by more than one plugin would only use one of them?  Maybe something like this?

   // In FOO plugin

   // In BAR plugin

This could then be documented on a wiki page at WordPress.org and become a best practice for plugin development maybe?

-Mike Schinkel

More information about the wp-hackers mailing list