Man, the WordPress Codex is deep. I've spent more hours than I'd like to admit plumbing its depths, and I still get the sense I haven't seen more than a tenth of what it has to offer.
Here's a function straight from the codex that'll be a big help to theme and plugin developers: the wp_enqueue_script() function.
What's it for?
Glad you asked. If you find yourself building WordPress themes and plugins that're heavy on the JavaScript, this guy's gonna be your new best friend. For starters, instead of having to package your theme with its own copy of jQuery or Scriptaculous, you can use wp_enqueue_script() to include the most recent version of the library WordPress has available wherever you need it. Calling the function thusly will print a link to the script right before the end of your document's head:
- <?php wp_enqueue_script('jquery'); ?>
But what if I prefer to include jQuery with my plugin anyway?
Nothing's stopping you. But consider the possibility that the people who use your plugin or theme are likely to at some point install some other WordPress add-on that also uses jQuery or Scriptaculous. The big benefit to using wp_enqueue_script() is that WordPress keeps track of what JavaScript is already being included on the page. No library or script will be included more than once. This can help cut down on collisions that might break your theme or plugin.
Cool. What else can it do?
Aside from JavaScript libraries, this function also gives you access to a bunch of other handy scripts already used by WordPress core, mostly in the admin interface. Check out the Codex page for a complete list. Even more helpful: you can register your own script – either something you wrote or borrowed from someone else – and declare any libraries on which it depends. Calling the function like this:
- <?php wp_enqueue_script('dropdowns', '/wp-content/plugins/fancydropdowns/js/dropdowns.js', array('jquery'), '1.0' ); ?>
would automatically pull in links to both your script and the jQuery library. All you're giving it is what you're calling your script, the path where it can be found, its dependencies, and your script's version number, in that order.
I don't pick apart a ton of WordPress plugins, but having looked at the internals of a crazy number of themes in the last year, I can tell you that a bunch of them could benefit from this handy function.






Comments
May 11, 2009
4:20 am
May 14, 2009
7:32 pm
May 15, 2009
12:51 am
May 19, 2009
5:23 am
June 15, 2009
3:00 am
June 16, 2009
5:36 am
July 11, 2009
11:51 am
August 8, 2009
12:44 am
August 14, 2009
3:40 am
August 26, 2009
10:54 pm
November 13, 2009
10:55 am
December 12, 2009
2:46 am
February 4, 2010
2:42 pm
February 22, 2010
9:11 am
March 11, 2010
3:45 am
March 27, 2010
4:06 pm
March 27, 2010
5:09 pm
March 28, 2010
6:41 am
March 31, 2010
9:49 am
March 31, 2010
11:33 pm
Whaddya think?