jQuery Window Name Transport Plugin

Yesterday I’ve released a jQuery window name transport plugin: jquery-wnt.js

Inside it’s much simpler than other attempts.

Outside it exposes two methods:

  • $.wnt.get( url, data, callback )
  • $.wnt.post( url, data, callback )

The arguments are:

  • url: the url you want to fetch
  • data: the data you want to send, as a js object
  • callback: the function that will take care of the response, received as the only argument

It works in IE8, FF3.5, Safari 4, Chrome 3, and Opera 10.

Known issues:

  • the query string of the url of the get is not honored, so you should pass those parameters as data
    FIXED 2009-10-07

WNT pages are here and here.

Counting, repeating, and unique ids

Here are some micro jQuery plugins that implement JavaScript closures, for object oriented pleasure.

Repeat Something n Times

{[ .repeat | .hilite( =javascript= ) ]}

New Date Value (unique id)

{[ .newdate | .hilite( =javascript= ) ]}

Counter

{[ .counter | .hilite( =javascript= ) ]}

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.