Using Javascript libraries with your plugin or theme

When you develop a WordPress plugin or theme, you might want to make use of some of the Javascript libraries distributed with the WordPress package such as Prototype, Scriptaculous and jQuery.

Simple enough, they are in the “wp-includes/js/” folder, so what more is there to it? Can’t I just add a SCRIPT tag in the HEAD section (or any other part of the WordPress page for that matter)? Of course you can do that, but you’ll most likely be causing a headache for other plugin and theme developers out there since you’re not following WordPress conventions as you should when you simply dump a SCRIPT tag into the content of the page.

This is the mistake that most plugin developers make. They usually make use of the “wp_head” action hook and inject SCRIPT tags referencing these declared Javascript libraries into the HEAD section of the WordPress pages. Problem is that when you do this, and another plugin has already referenced any of these libraries, you are redeclaring them which results in a Javascript error on the page, terminating execution of any other Javascript on the page (both internally and externally).

So what is the correct way to do this? WordPress provides us (developers) with a function calledwp_enqueue_script which allows you to safely reference the provided Javascript libraries and frameworks.

The first parameter of this function is the handle/name of the script. For example, if you wanted to reference the Prototype library, you would do something like below.

<?php wp_enqueue_script(‘prototype’); ?>

…where “prototype” could be replaced with any of the handles in the list below.

  • scriptaculous-root
    The Scriptaculous root without any of its sub-framework dependencies
  • jquery
    The jQuery library
  • thickbox
    Thickbox library, allowing you to display images, iframes, etc… in an overlay.
  • tiny_mce
    The tinyMce script which creates and powers the WYSIWYG HTML editor provided with WordPress

…and a few more. For a full list of all the scripts distributed with WordPress, together with the handle for each script, visit the wp_enqueue_script documentation at the WordPress.org codex wiki.

Of course, you can add your own scripts as well, whether they are scripts you wrote yourself or other scripts distributed publicly, but not included with WordPress. When you add your own scripts, please ensure that you specify the second parameter (explained below) of the wp_enqueue_scriptfunction so that WordPress will know what the location of the script is.

The second parameter of the wp_enqueue_script function allows you to specify the source of the script, meaning the location of the script file, relevant to the root of your WordPress installation. For example, lets say that I placed a script with the filename “myscript.js” into the folder of a plugin I’m coding, where the folder name is “myplugin”, the value for the second parameter of the wp_enqueue_script function will be “/wp-content/plugins/myplugin/myscript.js”. See the example below.

<?php wp_enqueue_script(‘myscript’, ‘/wp-content/plugins/myplugin/myscript.js’); ?>

The third parameter of the wp_enqueue_script function should always be an array (when specified). It tells WordPress which other scripts this script being enqueued depends on so that those scripts can be loaded before this one is loaded. For example, a custom script I wrote which I’m enqueueing is dependent on the Prototype library and the Scriptaculous framework. Therefore, I will specify them as dependencies. See the example below.

<?php wp_enqueue_script(‘myscript’, ‘/wp-content/plugins/myplugin/myscript.js’, <a href="http://www.php.net/array">array</a>(‘prototype’, ‘scriptaculous’)); ?>

The last parameter of the wp_enqueue_script function specifies the version number of the script being enqueued. This helps WordPress to always load the newest/latest script in case there are multiple, similar scripts which are different versions. See the example below.

<?php wp_enqueue_script(‘myscript’, ‘/wp-content/plugins/myplugin/myscript.js’, <a href="http://www.php.net/array">array</a>(‘scriptaculous’), ’1.0′); ?>

That’s all there is to it! No SCRIPT tags, no fuss!!! Please be considerate to other developers (and their plugins/themes) and use WordPress conventions as they were intended to be used.

http://codex.wordpress.org/Function_Reference/wp_enqueue_script

Revisions

There are no revisions for this post.

Tags: , ,

No comments yet.

Leave a Reply