WordPress Plugin: Random Posts Widget

Posted by on Wed, May 2, 2007

▼ A d v e r t i s e m e n t

My better half was complaining that she can’t find a suitable plugin for displaying random posts on her sidebar. I tried to look myself, and there were some reference to the plugin but nowhere to be found. One good plugin I found is Random categories with random posts by Mike Stickel but it uses categories which is not what we are looking for. Also, it requires tinkering with PHP codes which is not suitable for less technical bloggers.

So I went to analyze his code for a little while. What the heck, I’ll just create a simple widget version with no categories involved. So I took about 30 minutes to write this simple code with not so many options. It is a widget and configurable in the Sidebar Widgets page:

Random Posts Widget configuration

  • Widget title: the title of the widget
  • List types: ul for bulleted list, p for paragraph, br for paragraph with line breaks
  • Before title and After title: you have to find out what your theme uses before and after the widget headers. A simple way to do this is to identify a currently existing widget in your site and viewing the source. Find the title and see what is printed before and after. For example it might show as <h2>Widget Title</h2> which is used by most themes.
  • Post count: Number of random posts you would like to be displayed

Basically that’s it. It’s really a simple widget.

I don’t like to reinvent the wheel but since I can’t find any I just have to create one myself. Most probably I will have not so much time to maintain this plugin, but it is suitable if you are looking for a (very) simple plugin to display random posts.

You can download it from WordPress Plugins Directory: Random Posts Widget, or view online here: randomposts.php

Current Version: 1.5.0

Installation

Copy the file to the wp-content/plugins/widgets directory and activate it in the Plugins page. Then drag and configure it in the Sidebar Widgets page.

Warning: Will only work on widgets enabled blogs.

Change Log

  • 07-May-2007: Fixed a bug of displaying widget before and after tag
  • 14-Jun-2007: Fixed a bug where pages are also selected. Now it only displays posts.
  • 26-Jun-2007: Removed before and after configuration options, now taken from theme settings. Was broken for some themes with embedded class in the header tags. Reported by Guohua. Thanks.
  • 03-Aug-2007: Overhauled entire engine to optimize queries. This has been tested on a test blog with 50,000 posts on an old machine (Pentium III 600MHz) in maximum 3 seconds. Also removed duplicated results and added category to choose.
  • 04-Aug-2007: Fixed a bug where wrong permalink was displayed.
  • 10-Aug-2007: Fixed a bug where if 1 post count is selected an error occurred.
  • 27-Sep-2007: Added ability to detect database version (WordPress 2.3 has database changed) while maintaining backward-compatibility with 2.2.x

If you find this plugin useful, feel free to


Related Posts with Thumbnails

Tags: ,

Facebook Comment

WARNING & REMINDER: Comments are moderated and there is no exception. Comments unrelated to the post, too short, using phoney emails or funny names, will be marked as spam. Foul languages are filtered as spam. Your email will never be displayed on this site. If you would like to receive replies, use a valid email address and check the subscription box below.
 
  • can this be used on wordpress 2.5? if not can you make a version for it? thankss

  • ady

    Yep. It works on 2.5.

    There are a lot of plans for this plugin but it has to wait for a while due to other commitments.

  • my goodness… i’ve been looking for this type of plugin throughout the whole worldwideweb – and i found this post from google. you didnt tell me you have this plugin.

    thanks a bunch ady

  • ady

    Well, sometimes the thing are you looking for is under your own nose and you just don’t realize it 😉

    Enjoy… you’re lucky you can ask for face to face support for this plugin if you have difficulties LOL.

    Hmmm… I must find time to enhance this plugin. It’s long overdue.

  • Pingback: Was soll in die Sidebar? | Das Web des Jan()

  • Bobby

    Thanks for this plugin.

    FYI, I used “php echo ara_random_posts($before_title,$after_title); ” with WordPress 2.6.2 and it seems to work fine.

    I’ll let you know if I have to face issues.

  • Pingback: Random Posts in your wordpress blog | MyPTSMail Blog : Wiki,Email,Hosting Info()

  • ken

    excellent plugin! fantastic work…

    one quick question, ady–how difficult is it to increase the number of results to 15 instead of 10 max?

    i tried nosing around the code but i’m no programmer i’m afraid. 🙁

    thanks!

    ken

  • thanks a lot!

  • Pingback: 27 плагинов для wordpress которые я использую | Мои мысли()

  • Nice work.. !
    Just a newbie around, so I just want to ask a simple thing: how can I enable widget on my WP blogs ?

    Thanks.

    • ady

      Abiyanto, you’re using WordPress 2.0.2 so you need to install a plugin. Widgets are included by default in 2.2 or later.

      If you decide to stay with your current version you can download the plugin here: http://automattic.com/code/widgets/

  • Hi, i installed your plugin but as i’m using jLanguage to make posts in english and spanish your plugin show the post titles as [english]Title[/english][spanish]Titulo[/spanish].

    That is the syntax jLanguage uses to know which language to show but the default Recent Posts widget of WordPress show the titles correctly.

    Is there anything you or i can do to solve this? Many thanks.

    Regards.

  • This plug-in works with WordPress 2.7.1 ! Simple and smart !
    Thanks, that’s great !

  • This is very user friendly – thank you! I have gone through a number of plugins to try and find one that will also let me include an excerpt. Would it be difficult to include that in this plugin? I can edit it, but I don’t know where.

    Thanks so much!

  • Thank for make this plugin. How to exclude post in display???

  • Im on wordpress 2.8.6 and your widget has destroyed my blog

    I get this code when i try to acess any part of it, including admin

    Fatal error: Cannot redeclare widget_ara_randomposts() (previously declared in /websites/123reg/LinuxPackage21/mi/sc/mu/miscmusic.net/public_html/wp-content/plugins/random-posts-widget/randomposts.php:198) in /websites/123reg/LinuxPackage21/mi/sc/mu/miscmusic.net/public_html/wp-content/plugins/random-posts-widget/randomposts.php on line 198

    Please get back to me

    Thanks

    • ady

      Delete the plugin file randomposts.php (or the whole folder). As mentioned in http://wordpress.org/extend/plugins/random-posts-widget/ this plugin has only been tested up to 2.3 (although it worked up to 2.7). After you delete you should be back to normal and be able to access your site normally.

      I have intentions to update this plugin but still struggling to find time. WP 2.9 has a lot of changes as well, and this plugin definitely needs to be updated as it will be broken.

      Thanks for reporting the issue.

  • Hi. This might sound odd- but i’ve uploaded it and activated it and it is nowhere to be seen when i then view plugins or widgets. Hmmm. I’ve then tried to upload again and there is an error saying that it already exists.

    Any ideas? I’m using WP 2.9.

  • This is a great plugin. I’ve had it on my site for many months. I set it and forget it.

    But I’m with reader ken, above, who would like to increase the number of displayed posts to more than 10.

    I’m willing to dig into the style sheet and make changes.

    Thanks.

  • Diana

    Hello!

    This plugin is very nice. But I´m going mad trying to find a plugin or code to randomize post within a specified tag or category..
    For exemple,

    I have 3 posts: Number 1, Number 2, Number 3 under “Lucky Numbers´category”

    And a link somewhere: “whats your lucky number today” – so when clicked, the random code is able to retrieve a post within “Lucky Numbers´category” only.

    It could be very usefull if able to retrive posts within Tags, for a more specific content control.

    If someone else know some plugins or wp/code for this, please let me know!

  • Subhramani

    Hi.. I loved this widget. Thanks a million. Just one question. Can I make it NOT link up to the original article?

    I just want the widget to show up the posts and thats it.
    Please let me know.

    Thanks again 🙂

  • Thanks for this plugin !

  • @Diana….and to all others it may concern (and of course Ady)

    I was also looking for a plugin that could display some random posts from a specific category, as to me this is great for more dynamic sidebar content and CMS based sites. So after looking around a bit I found this handy little plugin, and another one called Get-a-Post.

    Still not quite what I needed so I’ve adapted Ady’s code in a way that allows it’s output to be plugged straight in to the get-a-post plugin! Used in a suitable code loop and voi-la, multiple random posts wherever you need them – although familiarity with PHP is needed so this is most useful to template designers and coders.

    I haven’t ‘published’ it yet as much of the code is Ady’s, and with it’s dependence on the Get-a-Post plugin I felt I should inform both authors first.

    Also this would be my first plugin so I don’t really know what to do in terms of making it available to others, however as long as both respective authors are happy then I’ll see what I can do…

    Thanks

  • I’d like to thank you, a great plugin to show a random post.