Random Image Block

The Random Image Block is a small plugin that will display a random image from your native WordPress photo galley or in-beaded images.

This widget will display the thumbnail of the random image, the “caption” and the images parent posts name. You may show all pictures on your site, or limit the selection to a single category if you wish. Once installed on your site, it will fully conform to the current theme. The Widgets title is also fully configurable. Random Image Widget was designed with full internationalization in mind and can be fully translated (Any help on this would be appreciated). As of Version 0.3 this plugin works out of the box without any configuraion (assuming you have pictures on your site).

The Random Image Block plugin works with WordPress 3.0+ in both single and multi site modes. As a Site Admin, you may activate this plugin across all the sites on your install.

This Plugin is fully translated into the following languages:

  • Arabic
  • Czech
  • Finnish
  • French
  • Danish
  • Dutch
  • German
  • Indonesian
  • Italian
  • Portuguese
  • Russian
  • Spanish

If you would like to help translating this plugin, or you see a problem with the current translation, please see my Translation page, and/or contact me.


Latest version: Download Random Image Block v0.10 [zip]


As with most WordPress plugins, there is two ways of installing this plugin.

Primary Option

  1. Go to your WordPress Dashboard and login as an Admin
  2. From your Dashboard go to Plugins section on the left hand side and select Add New.
  3. Search for Random Image Block
  4. Click the Install Now link and follow the instructions.

Secondary Option

  1. Download the latest version from the download page (https://wordpress.org/extend/plugins/random-image-block/)
  2. Extract the zip file and copy the folder “random-image-block” into the “wp-content/plugins/” directory in your WordPress installation.
  3. Activate the plugin from your Dashboard by going to Plugins -> Installed page.

Screen Shots

  1. The Random Image Block on the front page, conforming to the current theme.

    The Random Image Block on the front page, conforming to the current theme.

  2. The Wiget Admin page for the Random Image Block.

    The Wiget Admin page for the Random Image Block.


Q. Q: May I have more the one image on my sidebar?
A. A: Sure, just add a second or third widget to the sidebar, but you can’t do it from within this widget.

Q. Q: May I display more then one category at once?
A. A: No, you may only display a single category per widget

Q. Q: I have no picture in my sidebar, the widget doesn’t work!
A. A: Make sure the category you have selected has pictures in it, if it doesn’t, nothing will be displayed.

Q. Q: Will this plugin work with WP Super Cache enabled?
A. A: Unfortunately, no. WP Super Cache caches all php built html pages for quicker page loads. Since the Random Image Block is built directly into the html page, with WP Super Cache enabled, you will see the same random image on the same page until the cache refreshes. Each page will still have a diffrent image, but they will not update for each page refresh. Currently there is no work around.


Version 0.10

  • removed depressed calls to attribute_escape

Version 0.9.2

  • Fully tested for WordPress version 3.1

Version 0.9.1

  • Fixed bug where default options were always on.

Version 0.9

  • Added Advanced Options
  • Allow for custom Meta data
  • Added Tranlation for: Arabic, Czech, Danish, Dutch, Finnish, Indonesian, & Russian

Version 0.8

  • Added ability to link to the album vs the image.

Version 0.7

  • Changed to dropdown box for category selection, translaion files have not been fully updated, yet.

Version 0.6

  • Added ability to center image in the wiget area

Version 0.5

  • Switched to ‘{$before_widget}{$before_title}’… to try and resolve some display problems

Version 0.4

  • Translated into: French, German, Italian, Portuguese, & Spanish

Version 0.3

  • Fixed bug that showed no picture if the category box was left blank.
  • Single Category check box now works.

Version 0.2

  • Added check box to allow single category.

Version 0.1

  • Everythings new!

84 thoughts on “Random Image Block

  1. Hi Matt,
    I’m currently working on my website. This plugin is exactly what I wanted except with unlimited or selectable categories. Do you have any plans on developing this feature?


  2. Hi Matt,

    Thanks for the great plugin. It’s also exactly what I wanted for this site I’m converting to WP. I’m wondering how you configure to show landscape and portrait views as you have on your site?


    Pat K.

    • Hi Pat,
      I’m glad that I could help, and that this plug-in is ‘exactly’ what you needed! That’s always good to hear!

      As far as having landscape and portrait images, I think you have your squire thumbnails enabled. Check the “Media” page from your Dashboard’s “Settings” menu. You should see a check box under the thumbnails sizes labelled as “Crop thumbnail to exact dimensions”. Uncheck the box. Once it’s unchecked, all newly uploaded images will be landscape/portrait.

      To update the pictures already on your site, check out the plugin “Regenerate Thumbnails” by Viper007Bond. After you have rebuilt your thumbnails, you should have what your looking for.


  3. excellent widget, perfect for my customers site….being new to WordPress, you say assign it will show images from a category ? i guess the image has to be in a post assigned to a category right ? or have i missed something in assigning just an image to a category ? if so how and where do you do that ?

  4. On the front end, this works perfectly. When I edit links in the admin however, I get an error, something about modifying headers. Deactivating the plugin doesn’t work to make the error disappear, I have to completely delete it from the plugins folder. This is the only random image plugin I’ve found that works on the front end EXACTLY how I want, I’d be thrilled to know if this is fixable! Thanks!

    • Amy,
      I’m glad to hear that my plugin is what your looking for, but I’m sorry your having problems.
      When you say you “edit links”, what links are you editing? Under the links section on the left side bar? What version of WordPress are you using? What’s the error message?

      • Strangely enough (but I’m not complaining!), I reinstalled it to get the error message and it works now. I’m not sure what caused it before. I made 2 small changes to the php (removed the title and changed the word “Album” to “From”) so I thought that maybe was it, but I just made those changes and it’s working now.

        • Wonderful, I’m glad it’s working.

          The next version will allow you to change the word “Album” directly from the widget screen, didn’t think about removing the title, I should add that also.

          Thanks for the ideas, and I’m glad its working for you now.

  5. This widget is just what I was looking for.

    I am still putting my site together, but it looks like this widget is not inheriting the formatting of the other widgets.(ie background, borders, etc) Is it something on my end?

    • David,
      It looks like you theme is using the class “side_roll” instead of “widget” as WordPress suggests. This isn’t a problem until you install a plugin such as mine that doesn’t use the “side_roll” class.

      To resolve this, you should be able to modify your themes “style-Stone.css” file and add “widget” after “.side_roll” around line 359 so it reads
      .side_roll, .widget {
      If that dosen’t work, copy the block and replace “.side_roll” with “.widget”

  6. Matt, I love the plugin. When it works, it does exactly what I want. However, I occassionally get the following error on page load:
    Fatal error: Cannot use string offset as an array in /home/rgi/public_html/izzychronicle/wp-content/plugins/random-image-block/random-image-block.php on line 74
    I’m just using it to display all images from the site (not any one category). I was getting the error when I first installed it, but I went through my media library and deleted all the images that were just in the library (and not attached to posts). However, now it gives me the error when it is attempting to show an image from posts that have been made since I added the plugin. Any suggestions?

    • Greg,
      I really have no idea why your getting this error.

      I think it’s having a problem getting some meta data from the image in question. Try this, replace line 74 from:
      echo “<img width='”.$meta[‘sizes’][‘thumbnail’][‘width’].”‘ height='”.$meta[‘sizes’][‘thumbnail’][‘height’].”‘ src='”.wp_get_attachment_thumb_url($imgid).”‘ alt=’Random image: “.$attachment->post_title.”‘ />”;
      echo “<img src='”.wp_get_attachment_thumb_url($imgid).”‘ />”;
      If you have any problems, you can always just delete the plugin and reinstall it, and if you do, do *all* the images work then?

      • Matt,

        Thanks for your quick reply! Sorry I took so long to get back to this — I was out of town and so haven’t worked on the site for a while.

        Anyways, I replaced line 74 with the line you gave me, and it worked, but I would like to use a little bigger image than the 150×150 that requesting the thumbnail gives me. For now, I just put in the line:
        echo “post_title.”‘ />”;, which gives me the full size image (I want a 325×325 block). Is there any way I can get the wordpress “medium” instead of “thumbnail”? That would really be the best solution for me. Thanks so much!

        • Sorry, I dont know what happened to my code on that last comment. Anyways, the line I used was this one:

          echo “post_title.”‘ />”;

          • Your differently on the correct track, trying echo wp_get_attachment_image($imgid, ‘large’); for medium size pictures, you should be able to just change the above to
            echo wp_get_attachment_image($imgid, ‘medium’);
            But you can always change the size of your thumbnail image but going to “Settings” -> “Media”. Once you have updated your thumbnail size, you will need to rebuild your images, there is a good plugin I use for this named, “Regenerate Thumbnails” by Viper007Bond.

  7. This is the closest I’ve yet found to what I want, after trying twenty or more problematic or error-generating WP plugins. But… it doesn’t respect the “pull images only from a particular posting category” setting. I tell it to use category 10, but it persists in using _any_ image from _anywhere_ on my blog?

    • Sorry, try putting in the category’s slug not ID. You should be able to find the slug in Posts -> Categories. The slug is usually just the category name but all lower case and dashes where any spaces may be.

  8. Matt,

    Excellent Plugin! Very Useful!

    I too seem to have a theme that isnt co-operating as it should. Can you point me in the right direction (or css file) so i can get the formatting too? (and centered)

    Thanks a lot, and thanks for the great plugin!

    • Tracy,
      I just released version 0.7 of the plugin. This release *should* resolve the problems that you and others have ran into. Please try version 0.7 and let me know.

      It should now follow your themes structure, assuming the theme declared what it wants the plugin to look like, the plugin should now form to it.

      • Thank you Matt!

        That updated worked Perfectly!

        I really have to commend you on the good work and thank you for providing top notch support for it too. That is something to be proud of.

        Thank you!

        • Wonderful, this was an update that I have been meaning to do for a while. So when you asked, I figured I mite as well get it out to you.

          I’m glad to hear its working.

      • Thanks,

        I’ve downloaded it and activated it using “all categories” though.

        I have a lot of sub categories. Maybe I missed it. Can I select multiple categories?

        • You didn’t miss anything, you may only have the one category at this time. I am working on allowing you to have as many as you have on your site. But it’s not working yet.

  9. I was wondering why nobody had written a plugin like this, so i was delighted when i found yours. Not perfect for my needs so i adapted it to put up multiple images in the widget and it worked very nicely and very simply. But, there are a few small problems with your plugin (v0.7) that you should look into.
    1. where an image is not attached to a post, it will stop the page from generating.
    2. the widget was adding about half a second to my page load time. checked it out and your use of ‘numberposts’ => -1, when getting the images in the database was creating massive sql queries, so if you can change numberposts to a small number, then it will help a lot. (I installed the WP-Devel plugin which allowed me to see the SQL queries)
    3. the line: if (in_category($riw_cat_id, $albumid)): when this was set to “all categories”, i was finding that it was only rejecting lots of album id’s. which was strange since all album id’s should have been accepted. (i didn’t look any further into it, as i just turned off the categories bit cos it was of no interest to me)

    hope this helps, and isn’t too confusing

    • MikeC,
      Thanks a lot for taking the time and giving me some input.

      Point 1, don’t know, need to look into it more. So the image isn’t loading or the whole page? If whole page, does the page load up to the random image, then stop?

      Point 2, this is something that I have known about for a while, but haven’t figured out a better way (maybe I should just set it to 5 or something small, then add an option to change it back to all posts when someone needs too). I actually had it set to 25 for a while, but got complaints, but I have a feeling more people are seeing the slowness your seeing, then were “missing” the image on random page loads.

      Point 3, The current way the categories are filtered is dumb (check to see if post has image, check to see if post is in category, if not, rerun loop), im actually surprised your the first to say anything about it. Currently Im looking at better ways of filtering what categories are displayed, that will allow for more then one at a time. But I wasn’t looking to necessary change the line of code, so maybe I should re think what needs to happen.

      I’m glad the plugin does what you want it to do, if you find any other questionable parts or have any other input, please let me know.

  10. Hi,
    @ first i want to thank u for this plugin it’s Grate ,
    just 1 problem .. my sidbar is Wide and when the pic shown on my sidbar it’s very small , can U tell me how can i edit the size of pic to fix it with my sidbar ?
    and another quastion , when somebody click on the pic on the sidbar , i want him to redirect him to the same post of the pic , not on the same pic,,,
    If u like to take a look on my site (adeab.com)
    thats it , i’m waiting your answer ,

    • I see that you have two option to try and get you images to fit the wide sidebar.

      1. You can increase the size of your thumbnails and rebuild them. You may need to play with the sizes a bit After you have increased the size of your thumbnails, to update the thumbnails already on your site, check out the plugin “Regenerate Thumbnails” by Viper007Bond.

      2. You may also modify the widgets code to use the medium size image instead of the thumbnail if your use them for something else. You will most likely still need to play with the sizes and rebuild the images to get it to look right.

      • I still don’t get it. You can’t assign a category to a page or to an image.

        I only want a handful of images in the rotation and not the entire media library.

        Also, this may be a WordPress oversight: Once you attach an image to a post or page, you can’t detach it.

        Any recommendations?

        • No you can’t add a category to a page or an image, both limits of WordPress. The best way I can think of for you to display pictures on the side bar, that you will still be able to view the full size image of, add pictures as a gallery to a post, but never add the [gallery] short code. I haven’t tried this yet, but I don’t see why it wouldn’t work. Let me know.

  11. Hi! How can I use the function in any place of my blog, instead of using it as widget?

    Could you please post an example code?

    Thank you!

    • At this point in time, you really can’t without just rebuilding the plugin and build your own function. It’s just to integrated into the WordPress widget functions.

      But if you really want to do this, here’s my (untested) idea of how you could.

      function random_image_block() {
      $riw_widget_title = “Image Title”;
      $riw_center = “on”;
      $riw_cat_id = “1”;
      global $wpdb;

      if ($riw_widget_title == ” “) {
      $riw_widget_title = __(‘Random Image’,’random-image-block’);

      if ($riw_center == “on”) {
      $riw_center_output = “align=center”;
      } else {
      $riw_center_output = “”;

      $args = array(
      ‘post_type’ => ‘attachment’,
      ‘post_mime_type’ => ‘image’,
      ‘numberposts’ => -1,
      ‘post_status’ => null,
      ‘post_parent’ => $post->ID,
      ‘orderby’ => ‘rand’

      $attachments = get_posts($args);
      $noimages = count($attachments);
      if ($attachments) {
      foreach ($attachments as $attachment) {
      if ( $riw_cat_id !== “-1” ) {
      $albumid = $attachment->post_parent;
      //echo “on”;
      } else {
      $albumid = $attachment->post_parent;
      foreach((get_the_category($albumid)) as $category) {
      $riw_cat_id = $category->cat_ID;


      if (in_category($riw_cat_id, $albumid)) {
      $imgid = $attachment->ID;
      $meta = wp_get_attachment_metadata($imgid);

      // construct the image
      echo “{$before_widget}{$before_title}$riw_widget_title{$after_title}”;
      echo “<div class=’random-image’>”;
      echo “<p class=’random-image-img’ $riw_center_output >”;
      echo “<a href=”.get_permalink( $imgid ).” rel=”nofollow”>”;
      echo “<img width='”.$meta[‘sizes’][‘thumbnail’][‘width’].”‘ height='”.$meta[‘sizes’][‘thumbnail’][‘height’].”‘ src='”.wp_get_attachment_thumb_url($imgid).”‘ alt=’Random image: “.$attachment-/>post_title.”‘ />”;
      echo “</a></p>”;
      echo “<p class=’random-image-caption’><strong>$attachment->post_excerpt</strong></p>”;
      echo “<p class=’random-image-album’><small>”.__(‘Album:’,’random-image-block’).” <a href=”.get_permalink( $albumid ).” rel=”nofollow”>”.get_the_title($albumid).”</a></small></p>”;
      echo “</div>”;
      echo $after_widget;

  12. I like this plugin. Nice and simple.

    Is there anyway to get the random pic to link to the post page and not the attachment page?


  13. Do the images have to be saved in the WordPress media library? I have embedded images from an external gallery in posts of a certain category, and they don’t show up in the widget.

  14. Matt,

    Thanks for this wonderful plugin. This is exactly what i am looking for to finish up my mobile blog to display the latest wallpaper from the wallpaper category

    In my blog i just not using any widget all my widget elements are basically embedded with sidebar itself for quick load and custom design that fit my need.

    Matt, I am just looking for the same. Could you please provide me a PHP code so that i can directly embed on the sidebar like what we display Akismet by just calling the function

    • Unfortunately, no. WP Super Cache caches all php built, html pages for quicker page loads. Since the Random Image Block is built directly into the html page, with WP Super Cache enabled, you will see the same random image on the same page until the cache refreshes. Each page will still have a diffrent image, but they will not update. Currently there is no work around.

      I think im going to add this to the FAQ since there’s really nothing I can do beside switching the whole thing to Javascript.

  15. Very nice plug-in which works out of the box though with one problem. On my site it displays ALL images rather then just the thumbnails. On some pages it shows the full sized image which of course then expands into the rest of the page. Any idea how to fix this? Otherwise its a perfect plug-in that would do exactly what I need – though like this, its a game killer for me.

  16. Hi Matt,

    Thanks for the great plugin. I think there is a bug; in advanced option the “save” button doesn’t save my preferences I checked…
    Any idea?

    • Sorry about that, yes it looks like the default options in version 0.9 are always stuck on as it was correcting null values incorrectly. 🙂

      This has been resolved in version 0.9.1

      Thanks for finding this bug.

  17. just wondering why I need to select a category… most of my images from my media library are attached to pages not posts and thus are not within any category 🙁

    I simply want to show a random picture from my media library. is that possible somehow?

    • You don’t need to select a category to display a random image, but you will need them attached to a post apposed to a page.

      Can you make a place holder post to store your images on?

  18. Hi Matt, thank you heaps for creating this. It’s brilliant. I’ve used it here…

    The 3×3 grid is actually my sidebar with 9 widgets placed in there!

    I’d like to use it again on the large image to the left. Any ideas?

    • I am sometimes get duplicates too. Would there be an easy way to stop the same photo coming up in two places at once?
      I have used another plugin for the large image on the left. It works similarly but displays a random image from the uploads folder. These are all different sizes and some are getting stretched which looks bad. I would rather generate a cropped image like your plugin does. Can you offer me any advice on this.

      So grateful for the plugin, thanks again. Alistair

  19. Hey Matt,
    The plugin sound like exactly what I’m looking for. I’m running into a problem installing through.
    Installing through the search for plugin feature hangs on installing plugin, although I suspect thats due to the immensely flacky ftp server I’m developing the page on.
    However, after I install manually and activate the plugin in the admin/plugin pannel…..well, it’s after this that I hit a snag. I open up the widget pannel to drag random image block to my sidepannel, but instead of it dragging, it outputs a question within the widget dialog (not the sidebar dialog). There’s no form or boxes, it just fills the screen with text where there shouldnt be text. Any idea if I can fix this somehow?

    Thanks in advance.

  20. Hi Matt,

    I’ve made three little corrections to the plugin which allow it to pass the W3C validator service, I thought I’d give them to you to see if you wanted to integrate them in a new version, basically changing ‘ into ” and making sure those were recognized as text in output by adding anti-slash in the appropriate places in //construct the image

              // construct the image
              echo "{$before_widget}{$before_title}$riw_widget_title{$after_title}";
              echo "";
                if ( $riw_display_title == "on" ) { echo "<strong>$attachment-&gt;post_title</strong>"; }
                echo "";
                echo "<a href=".get_permalink( $riw_image_link )." rel="nofollow">";
  21. Hi Matt,

    Thanks for a nice plugin. However, I am experiencing some troubles with it. On static pages, it displays the same photo every time the page is loaded. On blog pages, however, it works fine.

    Am I doing something wrong?

    Thanks in advance for any help!

  22. Hello this is kinda of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML. I’m starting a blog soon but have no coding know-how so I wanted to get guidance from someone with experience. Any help would be greatly appreciated!

  23. Thanks so much for this great plugin! I had been using Gallery2 and its related plugin but I am moving my site towards embedding images into the posts. Not having a related random image generator was a sore disappointment until I found the Random Image Block. Thanks!!

  24. This is an awesome plugin and very helpful. I have a real estate and use this plugin to display different agents on property pages. I would also like to add a contact me button under the random picture or next to it. Can you give me some pointers on where to insert it or any ideas? Thanks

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.