WordPress : 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 called wp_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_script function 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’, array(‘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’, array(’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.

, , , , , ,
scriptlancebannerpng

This post was written by:

Antonie Potgieter - who has written 46 posts on Lost-In-Code.

I (Antonie Potgieter) am a software engineer/web developer located in South Africa. My full-time work is the management of Tribulant Software and the development of its software packages.

Contact the author

9 Comments For This Post

  1. abubin Says:

    I want to call jquery and another own java script but where do I put the wp_enqueue_script? I read all the howto and guide about wp_enqueue_script but non of them tells where to put the script. Aren’t these guides supposed to be read by people who doesn’t already know what to do with them?

    So, where do I put the scripts? in the loop? in some header file? in some function file? directly into my blog? where? in the moon?

  2. lost-in-code Says:

    @abubin

    Hi there,

    It depends on what you are doing.

    If you are developing a theme, you could put it in the header.php file, anywhere before the wp_head function is executed.

    If you’re going to do this with a plugin, you could add an action hook to the wp_head action or alternatively just put it in your plugin file so that the wp_enqueue_script function is fired when your plugin is loaded.

    I hope that helps.

    Best,
    Antonie

  3. Graeme Says:

    I’ve created a “Garage Door Menu” using jquery and would like to insert it into the top of my wordpress blog, between the header and blog postings but I receive the following error:

    Warning: Division by zero in (folder structure/header.php)

  4. Magnus Jepson Says:

    Hey thanks for this post and the post in the WooThemes forum. I was not aware of this function and it might save us a few support headaches in the future, which is always nice :)

  5. Antonie Potgieter Says:

    @Magnus
    Its a pleasure. I’m glad you found it useful.

  6. Renato Says:

    Thanks for that! The most valuable part was the answer to abubin’s question!

  7. Andrew Says:

    I wish more plugin and theme developers were aware of this. It should really be the only way to include JavaScript on a Wordpress page. Almost all of my plugin support goes towards telling people they have a plugin that is screwing up the Scriptaculous includes.

  8. Miles Says:

    I’m looking to create a mootools powered navigation in my theme. Would this function also cover mootools?

    If so, what would it look like?

  9. Antonie Potgieter Says:

    @Miles

    Thank you for your comment. Yes, you can use MooTools with wp_enqueue_script as well.

    Since MooTools isn’t integrated into WordPress, you’ll need to download the MooTools library, put it into your plugin or theme folder and reference it will wp_enqueue_script.

    To enqueue it, you’ll need to do something like this :

    < ?php wp_enqeueue_script('mootools', '/wp-content/themes/my-theme/mootools.js', false, '1.2.1'); ?>

    That should do it. Just change the second parameter to the correct dirname and basename of where you uploaded the Mootools library.

    To ensure that it is being loaded properly, simply check your site’s source code and check whether it has been injected into the HEAD section where the wp_head template function is executed.

    All the best,
    Antonie

4 Trackbacks For This Post

  1. Surf Raporu 22 Haziran 2008 | Taylan Aktepe Says:

    [...] Wordpress : Using Javascript libraries with your plugin or theme [...]

  2. Wordpress : Using jQuery library with your plugin or theme Says:

    [...] which allows you to safely reference your Javascript scripts. I recently wrote an article entitled Using Javascript libraries with your Wordpress plugin or theme which explains the wp_enqueue_script [...]

  3. links for 2008-11-14 | Yostivanich.com Says:

    [...] WordPress : Using Javascript libraries with your plugin or theme (tags: javascript wordpress php jquery plugin webdesign webdevelopment) [...]

  4. Geek Week - This week jQuery tutorials, CSS3, & WordPress - PHP, XHTML, Web Design, Flash | JasonCypret.com Says:

    [...] WordPress Using Javascript Libraries With Your Plugin or Theme [...]

Leave a Reply