WordPress Ajax slim

August 18th, 2007

What I do like very much about WordPress is the slimness and lightweightness of the core system. Since prototype is bundled with WordPress, there is no need anymore for including an extra javascript library with plugins that require a little Ajax.

For example, if you want to change something on your page by backgrounding the request of fetching some data from the server, you could simply use the Ajax.Updater() call:

Let’s say we want to do a WordPress plugin, that gives us back the server time on request via ajax. Besides that we would wrap the output into some add_action(”init”,”our_function”), the important lines are:


if(!empty($_GET[’ajax’])) {
sleep(3);
echo date(”Y-m-d H:i:s”);
exit;
}

Which means: If we get the parameter ‘ajax’, wait for three seconds and print out the timestamp, afterwards exit the script. For this example I put these lines into a small php script called time.php, which does basically the same thing.

And now, here is the demonstration using Ajax.Updater() from the prototype library:

hit reload to get a timestamp from the server

reload

And these are the lines that comprise the implementation of Ajax.Updater() within this post:

<script src=”http://wordpress.designpraxis.at/wp-includes/js/prototype.js” type=”text/javascript”></script>
<p id=”dprx_ts”>hit reload to get a timestamp from the server</p>
<a href=”javascript:document.getElementById(’dprx_ts’).innerHTML = ‘Loading…’; new Ajax.Updater(’dprx_ts’,'http://wordpress.designpraxis.at/wp-content/uploads/2007/08/time.php’,{method: ‘get’, parameters:’ajax=1′});”>reload</a>

Three easy steps are required:

  1. include prototype.js
  2. have a DOM object with an id specified to work with
  3. call the Ajax.Updater with an event handler

Fazit:

Quite a number of WordPress plugins were developed before prototype was included with the WordPress core system and have their prototype & scriptaculous, sajax etc. libraries bundled. I used and still use sajax for some of my own plugins, just because I am used to work with it.

But for the sake of the slimness and lightweightness of WordPress I think it is time for a little re-engineering: Plugins should be updated to use prototype for ajax calls to reduce the amount of requests an loading time of WordPress.

Share This

Leave a Reply

For support questions, bug reports and troubleshooting regarding plugins from this site, please visit the forum.

See also:

Plugins in action: Mini-Slides

Turakraft uses Mini-Slides.

Plugins in action: Mini-Slides on timbuktoons.tv

Timbuktoons uses the Mini-Slides plugin.

This time it's Facebook

As being advised I did my Facebook page today ;)

Plugins in action: Find Us

KGALEWOOD, the Botswana Travel Community Podcast Channel uses Find Us.

Close
E-mail It