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
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:
- include prototype.js
- have a DOM object with an id specified to work with
- 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 ThisPosted by Roland Rust
File under: Wordpress Plugins
See also:
- Find Us in dutch language (January 13th, 2008)
- Plugins in action: Mini-Slides (November 19th, 2007)
- Demo Mode 1.2 released (November 18th, 2007)
- Plugins in action: Mini-Slides on timbuktoons.tv (November 17th, 2007)
- Demo Mode 1.1 released (November 9th, 2007)


Leave a Reply