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.
Download
Latest version: Download Random Image Block v0.10 [zip]
Installation
As with most WordPress plugins, there is two ways of installing this plugin.
Primary Option
- Go to your WordPress Dashboard and login as an Admin
- From your Dashboard go to
Plugins
section on the left hand side and selectAdd New
. - Search for
Random Image Block
- Click the
Install Now
link and follow the instructions.
Secondary Option
- Download the latest version from the download page (https://wordpress.org/extend/plugins/random-image-block/)
- Extract the zip file and copy the folder “random-image-block” into the “wp-content/plugins/” directory in your WordPress installation.
- Activate the plugin from your Dashboard by going to Plugins -> Installed page.
Screen Shots
The Random Image Block on the front page, conforming to the current theme.
The Wiget Admin page for the Random Image Block.
FAQ
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.
Changelog
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!
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?
Thanks.
Jean, I’m glad my plugin is of use for your.
Currently I am planing on having it either show for a single category, or all categories. Will this work for your needs?
I’m having a few problems getting the categories drop down function to work at this time, once that is working as I what it. You will be able to select a single category or uncheck a box and show for all categories.
Thats awesome. Hopefully this will be done before I launch my site.
Thanks Matt.
Jean,
Here you go, version 0.3 allows you to do just that, display all images on a site.
-Matt
Matt,
Check it out. http://www.cichlidexplorer.com/cgi-sys/suspendedpage.cgi click anywhere inside and looks at the sidebar.
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?
TIA
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.
Thanks
-Matt
Thanks a bunch for your help! It’s working great now.
Pat
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 ?
I’m sorry if that wasn’t clear. Yes the category is the Posts category, not the images category.
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.
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”
Thank you for the prompt reply and the code tip, that did the trick.
Also, good job on this widget, it works as described.
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.”‘ />”;
with:
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.”‘ />”;
It’s getting stripped out. Here:
http://pastie.org/826801
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.
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.
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.
-Matt
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!
-Tracy
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.
-Matt
Hi Matt,
I’m still using your plugin. I remember you were thinking implementing category selection. Just want to clarify so I can upgrade.
Thanks.
Yep, the category selection is now done via a drop down menu and not longer a text box. It should work well for you know.
Let me know if you have any problems.
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.
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
mike
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.
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 ,
Thanks
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.
How do I assign a category to an image in the library?
You don’t, you assign the category to the page the image is attached to, whether it’s in a gallery or directly embedded.
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.
Hello, thanks for this great plugin!, How Can I get a php call to reform manually my template?
Juan, I’m sorry but I don’t understand what your asking me. Can you please explain what your trying to do.
Sorry,
-Matt
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;
break;
}
}
}
}
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?
Thanks.
Added to the wanted feature to the list.
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.
Yes, the images must be in a WordPress native gallery.
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
The code in http://mattrude.com/projects/random-image-block/#comment-1083 should work for your problem also. But I still have never tested it.
Let me know how it goes.
Great plugin.
Is there any way to get this to work with wordpress supercache enabled?
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.
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.
My only guess on this would be that your thumbnails are missing or set to full size. What happens if you run the “Regenerate Thumbnails” plugin by Viper007Bond? This should rebuild all your thumbnails to the same size.
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.
-Matt
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?
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
Please tell me what plugin to load images from other website/domain ?
This plugin works well 🙂
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.
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
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!
Peter
Oh no! Sorry about that last comment. I figured out what was going wrong. Thanks for the GREAT plugin!
Peter
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!
Blogs are all use WYSIWYG editors, checkout http://wordpress.com or http://wordpress.org for more information.
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!!
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