How to load JavaScript in WordPress plugins

If you want to load some JavaScript files from your WordPress plugin you have at least two options.

Head hooks

When building up a page, just before the closing tag of the head element, a theme should call actions hooked on wp-head. Those actions only need to output what they want to include into the head element, like script files.

What if a theme does not do that? Well, I’d include it into the so called “known issues”. For example, my last WordPress plugin, won’t work in this case. I think it’s better not to find a workaround, so that theme authors understand that they must follow WordPress standards.

On the other hand, there is also an admin-head hook, which is the wp-head hook counterpart when building up admin pages. For example, you could use the following function.

{[ .load-into-head /chili-web.php ]}

Script API

WordPress provides also a good Script API that will let you do anything you want with script files, following WordPress standards. These are the main functions:

  1. wp_deregister_script
  2. wp_register_script
  3. wp_enqueue_script
  4. wp_print_scripts (action hook)
  5. print_scripts_array (filter hook)

The general idea is that you write a function for loading your scripts using wp_deregister_script, wp_register_script, and wp_enqueue_script, and hook it on wp_print_scripts. If you also need to fine tune the order in which files are loaded or if they have to be loaded at all, then you can write another function and hook it on print_scripts_array.

deregister, register, and enqueue

wp_register_script let’s you create an alias and define dependencies for each script file. If a script file with the same alias was already registered, then your registration is ignored. This is a policy for conflict resolution (the first registration wins) that may help you, if you know it.

wp_deregister_script let’s you remove an alias. If the alias you give doesn’t exist, nothing happens. Tipically you use this function for forcing a new registration of an already registered alias: first you deregister and then register again.

wp_enqueue_script prepares the loading of a script. If the script was registered with some dependencies, this function automatically prepares their loading too, recursively, making sure each script will be loaded only once and before any script depending on it.

For example, jQuery 1.2.3 is registered by WordPress 2.5, but let’s say that you want to always download the latest version. You could use the following function.

{[ .load-with-api /chili-web.php ]}

Keeping jQuery and Prototype compatible is a needed functionality, because WordPress uses both and they both use $ as a global symbol. In fact, the jQuery file packed with WordPress includes the compatibility setting.

Fine tuning

If you want your scripts to be loaded in a particular order, with one script before or after another, maybe with respect to one that was registered by WordPress itself, or by other plugins, then you can write a function for the print_scripts_array filter.

For example, to load the jquery_no_conflict file just after the jquery file, you can use the following function.

{[ .correct-order /chili-web.php ]}

My Own Theme

If you want to see all this in action, you can install the My Own Theme plugin.

17 Replies to “How to load JavaScript in WordPress plugins”

  1. can somebody please tell me if there’s a way to check if a script has been registered?
    at a certain point in the php code i have to do – in pseudo code –
    if !registered(“mootools”) echo “<script src=’mootools.js’></script>”
    thanks for any help

  2. Thanks,I have searched a lot to manage loading “jquery”(conflict problem),I’ll try this. Second in second textarea function name “load_with_api()” must be “myOwnTheme_init()” 😉

  3. I am experiencing a little problem when using jQuery to inject contents in a Worpdress page.

    Basically I’m using an external php file, that holds the code to create content being injected. The external file is called topposts.php.

    If the file is included via php (using include, or require) it works perfectly, but when loaded using JQuery an error is issued, with a “call to undefined function”.

    It looks like jQuery load is executed in such a way it doesn’t “see” WordPress at all.

    Code:

    ‘In index.php
    .
    .
    .

    .
    .
    .
    .

    jQuery.noConflict();
    jQuery(“#foo”).load(“/wp-content/themes/tgv1/topposts.php”, “”,
    function(responseText, textStatus, XMLHttpRequest) {
    if(textStatus == ‘error’) {
    jQuery(‘#foo’).html(‘There was an error making the AJAX request’);
    }});

    then

    ‘in topposts.php

    Any idea?

  4. Hello, I searched a lot but I couldn’t find any solution 🙁 I have problems when I try to load jquery. I have been developing a widget and I need to load my own jquery because I use jquery.cycle.all.min.v.2.65.js and it needs to jQuery v1.2.6.
    The problems are
    – when I load the widget in wordpress-2.5, 2.6 my widget doesn’t work because jquery version is older (although written v1.2.6, it doesnt work)
    So, I must load my own jquery, It worked but now there is another problem which if theme or plugin has loaded jquery, my widget doesn’t work because of crash(reloading jquery)
    I used noconflict and also I tried to load jquery by doing but they didn’t solve my problem. Do you have any idea what should I do? Thanks.. 🙂

    1.
    wp_enqueue_script(‘iyikidogdun_jquery’, $src = ‘/wp-content/plugins/iyikidogdun/jquery_iyikidogdun.js’, $ver = ‘1.3.2_iyikidogdun’ );
    wp_enqueue_script(‘jquery.cycle.iyikidogdun’,’/wp-content/plugins/iyikidogdun/jquery.cycle.all.min.v.2.65.js’,array(‘iyikidogdun_jquery’),$ver = ‘2.65_iyikidogdun’);

    2.(loading before $before_widget but although has been loaded,my widget doesn’t work)

  5. can somebody please tell me if there’s a way to check if a script has been registered?
    at a certain point in the php code i have to do – in pseudo code –
    if !registered(”mootools”) echo “”
    thanks for any help

  6. If I were to keep both functions inside a class, how would I apply the filter to the array if I would call it from my constructor?

    This doesnt work. Its saying its missing an argument, obviously.
    add_filter(‘print_scripts_array’, $this->jquery_no_conflict_follows_jquery());

  7. Thanks a lot! I am just learning Information.
    Php and this was very easy to follow and helped a lot.
    You really took time to explain every little bit.
    Thanks again…

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.