Building SAJAX Applications with Wordpress
The Simple AJAX Toolkit, or SAJAX for short, is a library you can use to quickly build AJAX applications in PHP, Perl, ASP, and many other languages.
Using SAJAX with Wordpress can be tricky, because if you don’t initialize SAJAX or handle client requests at the correct time you’ll break Wordpress, your SAJAX applications, or both, and this guide will help keep you from doing both.
If you haven’t done so already, you should strongly consider integrating a custom PHP library into Wordpress. This isn’t a prerequisite, but it allows you to easily integrate your own SAJAX functions into Wordpress.
Integrating SAJAX Initialization into Wordpress
To use SAJAX with Wordpress without a custom PHP library you’ll need to edit wp-config.php, but to use SAJAX with Wordpress with a custom PHP library you’ll need to edit lib/LIB.php.
The instructions below apply to both variations, but if you’re editing wp-config.php make sure you place your SAJAX code right before the ending ?> line.
- Include the SAJAX library with
include("Sajax.php");and initialize the SAJAX library withsajax_init(); - After initializing SAJAX you’ll want to include your SAJAX functions with the appropriate
sajax_export()declarations. - Once you’ve exported all of your SAJAX functions add
sajax_handle_client_request();to handle all SAJAX requests before they’re passed to Wordpress. If Wordpress tries to handle the request it’ll have no idea what to do and your SAJAX application won’t work!
Once you’ve got SAJAX integrated into Wordpress you’ll need to edit your template so that you can use the JavaScript SAJAX provides.
To do this you’ll need to add the following code to the template file(s) you wish to use SAJAX in:
<script type="text/javascript">
<?php sajax_show_javascript(); ?>
</script>
If you want to use SAJAX in every page of your site then consider placing this in header.php.
Using the steps above you can now start calling the SAJAX JavaScript functions to dynamically interact with your web server and update the page’s content.
More Information
For more information about SAJAX visit the official SAJAX website where the SAJAX FAQ should get you started.
If you’re building web applications you now have no reason to miss out on the interactivity AJAX provides you, as SAJAX and Wordpress make building AJAX applications a snap.
If you enjoyed this post, get free updates by email or RSS.

























Checkout These Related Posts
2 Comments so far »
Wild Bluff Matt said
on February 27 2007 @ 3:42 pm
Have you been able to build anything that you really liked with this set up so far?
Ryan J. Parker said
on February 27 2007 @ 4:13 pm
Of course… I like everything I build!
More specifically, I’ve built: a Team Travel Analyzer, a Poker Hand History Replayer, and a Poker Hand History to XML converter.
All of them use this setup.
Comment RSS · TrackBack URI
Leave a comment
Pingbacks/Trackbacks