Spawning 32,678 processes took an average of 3.06 s/process of CPU time and 4.01 s/process of wall-clock time LOL.

Follow me

AJAX Random Posts – a new WP plugin (of mine)

Firstly, I’d like to show off a bit: hey I’ve caught up with the crowd, now both of my sites (this one and Thica.net) are upgraded to WordPress 2.7 – well, let’s hope that it is not prone to new vulnerabilities. The most significant change is the admin section – for the first time in my life I have to admit that it looks even better than the front-end. Is it good, or is it bad (my baby), I don’t know, but if you are using my plugin(s) and are worried about the compatibility, rest assured: they all work in this new versions. That is, of course, unless you tell me that they’re not.

But hey, this post is not about WordPress 2.7! There are hundreds of blogs out there babbling about it already! If you’re a careful reader (definition: a careful reader is someone who reads something from the title on), you probably know it, I’ve managed to used some free time to write another WP plugin called AJAX Random Posts, and this post is more or less its homepage.

As its name is saying, AJAX Random Posts (AjaxRP) display a list of randomly chosen posts from your WordPress-powered blog. Yeah, it’s nothing new, such a concept is very popular and has been greatly done by several plugins before. The limitation of those plugins however, is the lack of cache plugins compatibility. For example, you install a random post plugin. Then a visitor comes to post A, and under a post he’s served with 5 other random ones, let’s say C, Y, M, K, and J. If you have WP-Super Cache enabled on your blog, the post A is cached for, like, one hour. During that one hour, any visitor that reads post A will be served with its cached version, with C, Y, M, K, and J following. You must agree with me, it’s not so random anymore :) .

So, the only difference my AjaxRP is making is its “AJAX” part. With AjaxRP, the random list is NOT generated along with post A. Instead, it makes an AJAX call to the server after the page is loaded (more precisely, after the DOM is ready) and retrieves a list of random posts. This way, it works regardless of whether or not you have a cache plugin installed. That means, each page refresh shows a new random list. And I guess that is what should be called “random”.

FAQ

Q: Do you provide a template tag/ post tag?
A: Absolutely. If you don’t want the random posts to be display after every post, just set “Automatically display the random posts in a single post page?” to “No” in the Settings page, and

  • Manually put <!–ajaxrp–> at the end of your post, or
  • Place this php code: <?php if (function_exists(‘ajaxrp’)) ajaxrp(); ?>
    at a proper place in your template’s single.php page

Q: Where can I find a demo?
A: Right here! Take a look at the end of this post.

Q: How do I support this plugin?
A: Please link back to this post or blog about it. Also I would appreciate a lot if you can rate this plugin 5 stars in the WordPress repository.

History

Full history can be found here.

Download

Head here.


  • Hi,

    nice plugin, thank you very much.

    Only one thing, please change get_settings to get_option :)

    have a nice day

    Chris

  • thank you very much for the update fix in 0.3 :)
    there is still one get_settings function in it ;)
    have a really good day
    Chris

  • Hi Chris, I will make it pure next version then ;)

  • Hi,
    I’m looking for a way to display recent posts of a particular category on a page, and I’m wondering if your plugin might be one way to do that. What I’m wanting to do is have a page on our church website called “Film and Faith”, which describes what that particular ministry is, and then at the bottom of the page, have a box with the most recent posts of movies we’ve watched and discussed. Maybe there is a simple way to do this without a plugin, I don’t know.
    If you could help, I’d be much obliged!

    Thanks,
    Stewart

  • Steward, this is outside of my plugin’s scope. If you know about coding, you may take a look at the function get_posts (notice the arguments $numberposts, $category/$category_name, and $orderby). Or you may want to check Recent Posts plugin for a much easier way.

  • I’ve setup your plugin but am having a problem. I have an area in my sidebar (which appears on every page) where I want to manually display one excerpt, but nothing appears there at all. Everything seems configured properly and I have included your above manual code — — and yet nothing renders between my outer divs. I’ve also changed the get_settings in your code to get_option to see if that makes a difference, but nothing. Any help?

  • Hi Chris,
    Are you placing the code <?php if (function_exists(‘ajaxrp’)) ajaxrp(); ?> or <!--ajaxrp--> ?

  • Is there a way to display a horizontal list of 1-5 random posts on my non-Wordpress home page?

    Thank you!

  • Hi Dave, I’m pretty sure there’s a way, but since this plugin is for WordPress only, that feature would be out of its scope…

  • No, my blog is Wordpress-based, but my blog is in a subdirectory.  I want to display the random posts on my home page, which is not part of my blog.  Is there a way for me to do a call like {?phpinclude(“http://www.domain.tld/blog/random_posts.php”);?} ?

    Thanks for the fast response!

  • Hi Dave,
    Just like almost all other WordPress plugins, mine relies on the WP functions and hooks to work. As your homepage is irrelevant to the WP core behind, I’m afraid my plugin won’t work in this case.
    However, I believe you can write some php code in the homepage and manually retrieve the random posts instead of using the prebuilt WP functions.

  • Hi,
    I’m interested in creating a message board that uses WordPress.
    Does your plugin display a post, then after a period time display another post for n number of posts?

  • hi. Thanks for plugin

    perfect.

    Regards

  • Hi,
    We’re trying to place the random posts in our sidebar and are having the same problem that Chris had.  We tried both pieces of code and have only ended up with this:

    “Random Posts

    Loading…
    {post}”

    I’m pretty new at all this stuff, maybe there is a quick easy fix.  Any suggestions?  Thanks.

  • Hi Lily, please note that the script should be put inside The Loop.

  • amazing -it works!   Thanks  for your quick help.
    Is there an easy way to stop it from taking images from the posts so that it only shows linked titles?

  • Great it works for you Lily. But I’m not sure why it’s taking images from your posts. What is the link to your site?

  • Figured it out.  We were using the Thumbnails for Excerpts plugin, which automatically displays a thumbnail, apparently indiscriminately.

    Thanks for your help.

  • Very nice plugin.

  • Hi, i added this plugin in sidebar,and everything is perfect on single post pages but on my homepage and pages nothing show, is there any way to display it in sidebar on homepage too?
    please help
    boban

  • Just upgraded to the newest version and…no more displaying of posts! Worked like a charm before the change last week, but now the post no longer renders. I have it set to show a single post in a red block on my sidebar (non-widget; manual coding) and loved your plug-in. Now, flummoxed. I checked out the code and cannot figure out why it will no longer work. Weird. Grr. Thoughts?

  • Chris, as you can see it still works for me… Could you please check if you’re using a div with ID “ajaxrp”, because the plugin would fill this div with the posts.
    And btw, your site is very stylish. Awesome :)

  • Hi I would like to add ‘random post’ to my original post showing in the main page (As Announcement). How can i do that?

  • nice?

  • Hi David, have you tried the tag?

  • Can u advice what tag should i add in the ‘post’ field?

  • nice plugin phoenix!  :)

    only idee… it is possible  to prefer radom postings again the actual tag clouds ?
    example:
    - when tags mostly used, then prefer radom postings for this tags

    Why this?  On my blog, tag cloud reflect my main interest’s.    

  • Thanks for this great plugin.After using this plugin in my site,the Pages/visit ratio dramatically increases.

  • Great idea for a plugin! I’m currently using the Random Posts plugin by Rob Marsh, because I can set it to show only the image and link to the article.  It’s the only one I’ve seen that does that.  But he seems to have stopped supporting it.  Any chance you can make yours do the same?  I’d love to have the Ajax function to get past my caching as well as an active author.

  • Hi Valerie, I don’t know about the images – where are they from? About the link, you can use {url} tag to “Formart each item in the list”.

  • The images are set via custom field when creating a post. Take a look at the sidebar on my site – see the Be Random area?  Each of those pics links to its associated post.  That’s what I’d like to achieve with your plugin.  Possible?

  • Sorry Valerie, I’m afraid custom fields have not been supported in this current version of Ajax Random Posts (yet). Definitely I will think about it in the next version.

  • Would be great to make a widget option as well. It’s no fun to have to harcode php code in a template file….then when you update it….or replace it get’s overwritten.

  • Yeah, just like Gurumustuk, I’ also wait for the widget option.Nice plugin though…

  • Great plugin! But somehow I can’t seem to get it to work on my index.php page. Is it restricted to single.php only? Thanks!

  • Hi, as for now the plugin uses a post’s ID to exclude it from the returned results, that’s why it only works with single.php. However, I’m thinking of a way to overcome this limitation. Hopefully it wouldn’t be a problem in the next version.

  • phoenix.heart,

    Hi, it would be great if it can also be used on other pages (index.php, category.php, page.php, etc). Would possibly, greatly increase page views.

    Looking forward to the next version!

  • That’s a great plugin

  • Chiming in again! It has increased page views on my site since search engine traffic is offered select posts from my information pool. It allows them to “hop around” my blog.

    Thanks! Will be sure to check out the next version once available!

  • Hi! I tried installing this awhile ago and when I tried testing it, it was taking too long to load one blog post. There was no error during the plugin activation.

    Thanks!

  • A widget for the sidebar would be nice

  • Excelent plugin, i write something about it on my blog. Congratulations!

  • Thanks, Minea :)

  • Thank u, very nice.

  • wow gr8 plugin

  • Hi,
    Plugins looks great. Just wanted to know if there is a way to exclude certain categories from the randomizer, or even better – let the randmize choose random posts from the same category of the current single post.
    Thanks

  • Thanks for this great plugin

  • so ,does this a test ~>?a demo?

You can follow any responses to this entry through the RSS 2.0 feed.

Trackbacks / Pingbacks