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.

  1. Include the SAJAX library with include("Sajax.php"); and initialize the SAJAX library with sajax_init();
  2. After initializing SAJAX you’ll want to include your SAJAX functions with the appropriate sajax_export() declarations.
  3. 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.

Tags: , , ,

If you enjoyed this post, get free updates by email or RSS.

Checkout These Related Posts

  • Integrating a Custom PHP Library into Wordpress
  • Using Wordpress to Maintain Blog Consistency
  • Using SEO Book’s Keyword Suggesiton Tool to Get Content Ideas for Your Website
  • Did You Remember to Update Your Wordpress Time Zone Offset for DST?
  • Fixing JavaScript Affiliate Links in Wordpress 2.1.1
  • 2 Comments so far »

    1. 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?

    2. Ryan J. Parker said

      on February 27 2007 @ 4:13 pm

      Of course… I like everything I build! :D

      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.

    3. Comment RSS · TrackBack URI

    Leave a comment

    Name: (Required)

    eMail: (Required)

    Website:

    Comment:

    Pingbacks/Trackbacks