Loading Javascript Libraries in Wordpress Plugins with wp_enqueue_script()
Have a lot of Wordpress plugins? You may need server web hosting if you’ve got limited server space. Whether you use Outlook or Exchange 2007 for your email, you can get it hosted at an outside company so that you save room on your own server. Outsourcing Microsoft Exchange will definitely save you time, money and server
space.
In the process of modifying my Pre-Publish Reminders plugin for WordPress, I came upon an instance where I wanted to use a javascript library that is packaged with WordPress by default, but I couldn’t figure out how. However, after a little bit of frustration, followed by a little bit of detective work, I found an elegant solution included in the WordPress core.
First, let me share with you what doesn’t work. I originally had planned on placing things in the admin <head> section like so:
function nfoppr_add_js_libs() {
?>
}
Now, this approach didn’t work. The reason is that ABSPATH has to do with the file system and not the URL of the blog. So you end up with something like:
... src="./home/usr/nickohrn/wp-includes/js/prototype.js" ..
and this will not include your script. So, I had to find an alternative that would work. Searching through the function reference on WordPress I came across a note about the WP_Scripts object. This looked promising so I started to dig into the core WordPress files. I ended up finding the function wp_enqueue_scripts() within /wp-includes/script_loader.php which allows you to add a script to the <head> section of the page very easily. The function call looks like this:
wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false );
If WordPress recognizes the handle you provide, the library will be added for you. If it doesn’t support calling the library by handle (or the library is something custom you wrote) you’ll have to supply the path to the file as a second argument. The third argument lists any dependencies that the library you are loading may have (in the form of an array or false if there are no dependences). Finally, the fourth argument is the script version which, according to the documentation is primarily for cache busting.
To use the function, you’d do something like this:
function nfoppr_add_js_libs() {
//Add the Prototype framework to the header
wp_enqueue_script('prototype');
wp_enqueue_script('my_custom_prototype_extension', 'path/to/my/lib', array('prototype'));
}
//When you're in the admin menu, add the javascript libraries
add_action('admin_menu', 'nfoppr_add_js_libs');
As I couldn’t find a comprehensive listing of what library names you can call, I’ve decided to include one here. In the following table you can find the full name and handle for each library that is include by default. If applicable, I’ve included a link to the script homepage for each of the libraries that noted one within their source. There is a separate list of libraries that can be included if the logged in user is an administrator, but I’ve left those off the list for now.
| Name | Calling Name |
|---|---|
| Docking Boxes | dbx |
| Fade Anything Technique | fat |
| Simple AJAX Code-Kit | sack |
| QuickTags | quicktags |
| ColorPicker | colorpicker |
| Tiny MCE | tiny_mce |
| WordPress Tiny MCE | wp_tiny_mce |
| Prototype Framework | prototype |
| Autosave | autosave |
| WordPress AJAX | wp-ajax |
| List Manipulation | listman |
| Scriptaculous Root | scriptaculous-root |
| Scriptaculous Builder | scriptaculous-builder |
| Scriptaculous Drag & Drop | scriptaculous-dragdrop |
| Scriptaculous Effects | scriptaculous-effects |
| Scriptaculous Slider | scriptaculous-slider |
| Scriptaculous Controls | scriptaculous-controls |
| Scriptaculous | scriptaculous |
| Image Cropper | cropper |
| jQuery | jquery |
So there you have it. I think this is a really elegant solution. Plus, it prevents you from loading the same javascript library multiple times if multiple plugins are using it. If you have any questions, feel free to ask me, and I’ll try to sort things out, or you can refer to the WordPress documentation (although it didn’t have much reference to this at all.)
April 18th, 2007
It’s amazing that this very cool, very powerful technique is undocumented. I want my money back! Oh wait, it’s free…
Thanks!
April 18th, 2007
Oh yeah, one more thing, you can also queue them up by using the function several times in a row, thus:
June 8th, 2007
Thanks for the documentation! Incidentally, to get the http wordpress path you’d use get_bloginfo(’wpurl’) instead of ABSPATH
June 9th, 2007
@Firas - Thanks for the info. I’m glad I could help in some small way.
June 18th, 2007
I use add_action(’wp_head’, ‘myFunction’) to load some stuff in the portion of the web page.
That function prints some stuff then calls wp_enqueue_script. The code is printed fine, but the “enqueued scripts” are not loaded. When I move out of the function (outside of any function), it works fine.
I looked at the code and don’t understand why this is not working.
July 4th, 2007
@Guillaume:
This guy
(http://zeo.unic.net.my/notes/lightbox2-for-wordpress/)
adds his wp_enqueue_script to the init() action. But I haven’t tested it.
August 4th, 2007
It is pity that such a wealth of information is not available from wordpress.org. I wish someone who has proper knowledge should go and fill that in.
I wish I had a better understanding of a number of API’s in wordpress. I see one of the most important query_posts is not documented properly. This wp_enque_script is another example.
September 8th, 2007
This is my first post
just saying HI
September 17th, 2007
@Guillaume: I seem to have the same problem. Have you managed to get to the bottom of it ?
September 18th, 2007
[...] reading Nick’s entry on Loading Javascript Libraries in Wordpress Plugins, I have decided to use the same techniques to get my custom Theme to load JavaScript using [...]
September 18th, 2007
This also seems to work for Themes and not just Plugins! I have this working for my theme on my site.
September 18th, 2007
@Azizur - That’s great! I’m glad this worked for you and I was able to contribute in some small part to the WordPress community.
November 4th, 2007
I am desperately seeking a solution to this. I cannot load my own version of jqeury, this is my first time making a plugin and I must have jacked about 10 plugin’s codes for loading javascript into the head and NONE work what the hell is the problem? When I view the source it shows the exact same route and path it would be as if I manually put it into header.php. I even tried the above method with enqueue script however when I attempt to supply my own name and path for it and put ‘jquery’ it decides to grab its own, anyone have a solution for this?
November 29th, 2007
@Guillaume, @Owen,
Calling wp_enqueue_script() in a “wp_head” action doesn’t work because wp_head happens after scripts are queued and processed. Placing it in a “init” action would work if you also want the script to be added to public pages, since init is called for every page. Placing it in a “admin_menu” action, as the article describes, resolves both issues.
December 15th, 2007
very interesting, but I don’t agree with you
Idetrorce
December 20th, 2007
I would like to see a continuation of the topic [edit: removed link to an obviously commercial website]
January 2nd, 2008
Please, add ‘jquery’ in the list of js
January 2nd, 2008
@Tassoman,
Will do.
January 16th, 2008
I found this article very helpful. Thanks.
January 16th, 2008
@Simon,
Thanks for the feedback :)
February 25th, 2008
[...] it in themes and plugins. This applies to every other JavaScript library in that folder as well. Reusing those files in themes and plugins can be done, but I’ve tried several times and ultimately can’t get it to work. Perhaps an official [...]
February 25th, 2008
[...] it in themes and plugins. This applies to every other JavaScript library in that folder as well. Reusing those files in themes and plugins can be done, but I’ve tried several times and ultimately can’t get it to work. Perhaps an official [...]
March 3rd, 2008
Thanks, this is just what I was trying to figure out.
March 14th, 2008
what’s the calling name for mootools?
April 12th, 2008
THANKS for this doc!
Although it doesn’t help with my current project, and I was only led here on a tangent investigation, it was very well written and I’m sure it will come in handy sometime!
Cheers!
July 3rd, 2008
add the wp_enqueue_script() to where in the codes? Anywhere? header? wp loop?
I tried adding to my page file and it doesn’t work.
July 3rd, 2008
@abubin - You would need to use wp_enqueue_script somewhere before the header is being added. You could put it at the top of your header.php file if you wanted, probably.