Ajax WordPress post popup with SimpleModal and jQuery
Today we are going to look at getting the content of a post using ajax and then display it in a popup box. The popup part is actually the easiest part, thanks to the fantastic Simple Modal plugin for jQuery written by Eric Martin.
So the basics here are:
- we call a page using ajax, passing the id of the post
- The page grabs the content of the post we requested and returns it to the script
- We then output the content in a div and “simplemodal” it
And for the result, click on this link.
The major problem when it comes to ajax and WordPress is that you need to call a php script that will have access to all the WordPress built in functions. The solution came from this post from John Faulds.
Basically you create a page template that will be in charge of grabbing the post from the id we pass and returning the content to the script.
This is going to be a simple loop like this:
<?php
/*
Template Name: Ajax Handler
*/
?>
<?php
$post = get_post($_GET['id']);
?>
<?php if ($post) : ?>
<?php setup_postdata($post); ?>
<div class="whatever">
<h2 class="entry-title"><?php the_title() ?></h2>
<div class="entry-content">
<?php the_content(); ?>
</div>
</div>
<?php endif; ?>
Save this code in a file called ajax-handler.php in you theme folder. Now if you go in your admin panel and create a new page, you should be able to select this template as the page template. So I have created a page in WordPress called Ajax Handler with the template “Ajax Handler” assigned to it. The special function we use here, get_post(), will grab the content of a post with the Id we’ve passed with ajax as a GET variable.
Talking about ajax, lets have a look at the function to do the stuff:
jQuery(document).ready(function() {
jQuery('a.postpopup').click(function(){
id = jQuery(this).attr('rel');
jQuery('<div id="ajax-popup"></div>').hide().appendTo('body').load('http://urlofyoursite.com/ajax-handler/?id='+id).modal();
return false;
});
});
note: I am using ‘jQuery’ instead of $ because I use the jQuery included in WordPress which is in no conflict mode. If you use normal jQuery then replace all occurrences of ‘jQuery’ by $ in my javascript snippet. This code would go in an external javascript file you would include on your page or in the header. But the first option is recommended.
This a very simple way to do it, thanks to jQuery. This means: when a click on a link with a class postpopup, get the id from its rel attribute, create a empty div, load the page ‘ajax-handler’ and add the content of the data returned to the div. Then SimpleModal it.
The html for the link should look like this:
<a href="javascript:;" rel="postid" class="postpopup">this link</a>
note: in this html it would be better to replace the href by the actual url of the post, so if someone hasn’t got javascript enable they can still view the content.
And that’s it, all done. To get everything to work you will of course need to include jQuery and SimpleModal on your page. Don’t forget to hide your ajax handler page from your blig navigation. If your theme is using the wp_list_pages function you can pass a parameter ‘exclude=id_of_the_page’ to exclude it from the list.
And for the result, click on this link.

11 Comments
Thanks! Bookmarked.
Hey, this looks awesome and it may be just what I was looking for. Tell me, can the pop up box be interactive – for instance, could I have my subscription form in there and have it be usable? Or will it only work with static content?
I haven’t worked with jQuery and SimpleModal before. Everything seems pretty self-explanatory except for the function you show.
Where do I put that?
To answer your first comment, yes I believe it would work with any kind of content but I haven’t tested it with anything else than normal post. I have updated the post so it should be clearer where the javascript function go (see the “note” section below the code).
Hope this helps.
I’m having trouble getting this to work. I am on WP 2.8.6. Here is what I did:
1. Created a page template called ajax-handler.php with the code you provided in your first code box, plus this code – . I’ve tried placing this just under the php template designation section, and also tried it within the loop. Uploaded it to the theme root.
2. Uploaded the jquery.simplemodal-1.3.3.min.js file into my theme js folder.
3. Created a js file containing the jquery function from your second code box (with my url corrected) and uploaded it into the theme js folder.
4. Created a link as follows – Subscribe
When I click on the link, it just takes me to the new page in the usual way. No pop up.
Can you tell me what I’m doing wrong? Thanks so much for your help!
Hi, I added another post since you posted this answer (see above). I’ve gone through all the steps as best I can, but it’s still not working. Can you look at what I’ve done (listed above step by step) and see if you can spot the error? Thanks for your help!
The only thing I can think of when I look at the steps you followed is that you haven’t called the two javascript files as well as jQuery in your template header. You have to specificly load them otherwise they wont get included on the page.
If you look at the source code of the this page you should see right at the bottom (line 399) where I call the gallery.js file, which holds the ajax function and SimpleModal.
Is it possible to fashion multiple links using this?
I would beleive so. My first test was using 3 links successfully. What have you got in mind?
Likewise, this isn’t working for me and I cannot work out what’s going wrong.
I have the Ajax Handler page saved, using the ajax-handler.php file as template. header.php is calling http://{mywebserver}/{theme}/js/popups.php file that has your jquery function and SimpleModal in it.
jQuery is being called like this:
http://digwp.com/2009/06/including-jquery-in-wordpress-the-right-way/
Then this is the link I’m using:
The post ID is correct, the functions are identical to yours, the files are all in the right places and have the right names, the link to popups.php is pointing to the correct location, but the class=”postpopup” link does nothing whatsoever. I’ve disabled all other plugins to make sure nothing is conflicting too.
@David Not too sure about what you mean with the popups.php? Or is it popups.js? Anyway, I would recommend checking in Firebug what’s going on or email me a link at admin@thisdomainname.