jQuery and Ajax in WordPress Plugins - Public Pages

My previous post teaches you how to use jQuery and Ajax for the administration pages in your WordPress plugins. To use them in your user-facing pages requires a few changes.

We’ll use here a simlarly contrived example. Let’s say you use <!--more--> in your longer posts so they don’t fill up too much of your page. Normally, clicking the “Read more…” (or whatever text you use) link takes the user to a separate page with the complete post. In our example, rather than sending the reader to a new page, we’ll make an Ajax request to get the rest of the post and insert it directly into the current page.

Step 1: A Standard WordPress Template

Somewhere in your template, you’ll find a section like:

<div class="entry">
  < ?php the_content('Read more...'); ?>
</div>

This displays the contents of your post, with the “Read more…” link at the end to read whatever was cut off by <!--more-->. Pretty simple.

Step 2: Get the Full Post

Now it’s time to start writing our plugin. We’ll call it “Post Expander”, so our plugin file will be called post-expander.php.

We need a function that will return just the contents of a post. For example:

function post_expander_show_post (  ) {
  if ( have_posts() ) {
    while ( have_posts() ) {
      the_post();
      the_content('Read more...');
    }
  }
  die();
}

This function will take advantage of the WordPress loop, printing out just the contents of the post before dying.

So how and when do we call this function? First, we use the init hook to see if the requested posted our special post_expander variable.

function post_expander_activate ( ) {
  if ( isset( $_POST["post_expander"] ) ) {
    add_action( 'wp', 'post_expander_show_post' );
  }
}
 
add_action('init', 'post_expander_activate');

If the variable is set, we attach post_expander_show_post to the wp hook, which runs after posts are loaded but before any templates are executed. Now, if a page is requested with the post_expander variable posted, this plugin will intercept the request.

Step 3: Write the JavaScript Function

So we need a JavaScript function to post that request and insert the full post into the page. We’ll put that code in post-expander.js.

jQuery(document).ready( function($) {
  $(".more-link").click( function() {
    var link = $(this);
    $.post(link.attr("href"), {
        post_expander: 1
      }, function(data) {
        link.parents(".entry").html($(data));
      }
    );
    return false;
  });
});

Pretty simple JavaScript, that. (NB: link.parents(".entry") makes the assumption that the body of your post is in a div with the class entry.)

Step 4: Load Your JavaScript into the Page

Just a bit of housekeeping left, now. We need to load our JavaScript into the page, taking care to mention that it requires jQuery.

function post_expander_list_scripts ( ) {
  wp_enqueue_script( "post-expander", path_join(WP_PLUGIN_URL, basename( dirname( __FILE__ ) )."/post-expander.js"), array( 'jquery' ) );
}
 
add_action('wp_print_scripts', 'post_expander_list_scripts');

Once again, this plugin is not entirely practical, but you’re welcome to download the full code.

14 Responses to "jQuery and Ajax in WordPress Plugins - Public Pages"

  1. [...] jQuery and Ajax in WordPress Plugins - Public Pages [...]

    How to Create A Sliding Panel with Jquery | Simply Net Dev | 2008-10-28 11:24 | Permalink

  2. [...] jQuery and Ajax in WordPress Plugins - Public Pages [...]

    CSS for jQuery Selectors - It really is just CSS | Simply Net Dev | 2008-11-07 18:50 | Permalink

  3. [...] View original post here: x + 3 - jQuery and Ajax in WordPress Plugins - Public Pages [...]

    Wordpress UK » x + 3 - jQuery and Ajax in WordPress Plugins - Public Pages | 2008-11-08 09:12 | Permalink

  4. [...] jQuery and Ajax in Wordpress plugins [...]

    The Ultimate Guide to Building a Wordpress Plugin - NETTUTS | 2008-12-29 21:49 | Permalink

  5. [...] jQuery and Ajax in Wordpress plugins [...]

    Cotton Rohrscheib - Blog Archive » Building a Wordpress Plugin | 2009-01-02 23:38 | Permalink

  6. steve | 2009-01-21 08:58 | Permalink

  7. [...] WordPress Eklentilerinde jQuery ve AJAX [...]

    Duymadim.com | 2009-02-02 10:00 | Permalink

  8. Do you have a working example to view?

    Bryan | 2009-02-17 13:17 | Permalink

  9. Excelente información, con esto creare un videotutorial. Saludos

    Polin | 2009-03-14 14:41 | Permalink

  10. [...] WordPress Eklentilerinde jQuery ve AJAX [...]

    » wordpress eklentisi olu?turma rehberi | 2009-03-15 14:13 | Permalink

  11. [...] WordPress Eklentilerinde jQuery ve AJAX [...]

    » wordpress eklentisi olu?turma rehberi | 2009-03-16 06:56 | Permalink

  12. [...] WordPress Eklentilerinde jQuery ve AJAX [...]

    WordPress Eklentisi Olu?turma Rehberi | Webmaster Ar?ivi | Script Download | Tema Download | 2009-05-20 08:47 | Permalink

  13. Great post - your little trick for hooking code in the runs after Wordpress is set up but before it outputs anything for providing the server-side of the AJAX call is perfect.

    I’ve also used it for plugins that generate image data, and / or downloadable files, e.g. CSV reports, XML downloads.

    It amazes me that this isn’t covered in the Codex!

    Great post.

    Lee Willis | 2009-12-15 06:45 | Permalink

  14. Could this be done with the comment links as well? CHEERS!

    poosk | 2009-12-17 09:04 | Permalink

Leave a Reply