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.

How to force jQuery.extend deep recursion

jQuery.extend extends a target object with properties from other objects and it’s widely used in every piece of jQuery code.

Really it’s very useful and simple to undestand and use for flat properties.

{[ .extend-flat | @how-to-highlight-code-in-wordpress.hilite( =javascript= ) ]}

On the contrary, if the involved objects have object properties, jQuery.extend is less intuitive and less useful too.

{[ .extend-object | @how-to-highlight-code-in-wordpress.hilite( =javascript= ) ]}

Luckily, an undocumented feature (deep) makes jQuery.extend recurr object properties.

{[ .extend-deep-std | @how-to-highlight-code-in-wordpress.hilite( =javascript= ) ]}

Unluckily, deep only works for the first level. (not really a ‘bug’)

{[ .extend-deep-bug | @how-to-highlight-code-in-wordpress.hilite( =javascript= ) ]}

If you need deep recursion use jQuery.extend_deep instead.

{[ .extend-deep-fix | @how-to-highlight-code-in-wordpress.hilite( =javascript= ) ]}

Here is the jQuery.extend_deep plugin:

{[ .extend-deep-plugin | @how-to-highlight-code-in-wordpress.hilite( =javascript= ) ]}