Random Image Plugin for WordPress

The latest version is 4.1, released September 26, 2007 (works with WordPress 2.5), and is still backwards compatible with 2.3, 2.2, 2.1, and 1.5

Inspiration

One of the things I’ve been thinking about more since starting at O’Reilly is how to draw attention to a back catalog of content. For a single blogger, it’s pretty easy to link back to a relevant old post because of the information retrieval processor built into our heads. But for a site managed by an editor who may not be as familiar with the back catalog, the older content will languish, accessible only by search engines.

Then the other day I discovered the most amazing thing. I was freshening up the layout of O’Reilly’s Mac DevCenter, and we decided it would be cool to have a randomly populated book sidebar. Which meant I needed to pull records out of our database in random order—not something I’d ever needed to do before. It turns out that the SQL needed to do this is astoundingly simple:

SELECT *
FROM table
ORDER BY RAND();

Maybe you only want a single random record? Easy.

SELECT *
FROM table
ORDER BY RAND()
LIMIT 1;

Seeing this in action for the first time is like a revelation.

What does this plugin do?

Since I started blogging, I’ve added 500+ images to my blog posts, and I’ve always wanted some way to index them, or make them accessible in aggregate. I wrote a script once that generated a webpage to display all the images, but I had no means (at the time) to link them back to their associated blog posts.

Of course since I moved to WordPress, I have complete control over my content, so I decided to write a plugin to look for blog posts with images and return one image at random, linking back to its post. Update: If you prefer, you can now configure this plugin to display images in reverse chronological order (most recent to least recent) rather than in random order.

You can see it in action in the upper-righthand corner of my homepage. Here’s a screenshot:

randomimage plugin for Wordpress screenshot

This plugin does not rotate randomly through a group of images stored in a single directory on your webserver. If you are looking for a plugin to do that, try Matt Mullenweg’s A Rotater Apart or Kamiel Martinet’s Wordpress random header plugin or MHough’s Header Image Rotator.

Want to use the plugin on your site? Be my guest. As always, please let me know if you have any problems or questions. I’m happy to help.

Instructions

  1. Download and unzip randomimage-4.1.zip (v4.1)
  2. Upload the file randomimage.php to your plugins directory:
    /path/to/wordpress/wp-content/plugins
  3. Activate the plugin (don’t forget!)
  4. Edit the theme template file index.php or sidebar.php in your theme’s directory:
    /path/to/wordpress/wp-content/themes/name-of-theme
  5. Add the following line of code where you want a random image to appear:
    <?php randomimage(); ?>
  6. Configure the plugin from within WordPress under Options > Random Image

Here’s a screenshot of the configuration interface (new in v2.0).
Random Image plugin for WordPress' configuration interface

How to Include Random Images across Blogs

People have often asked, “How do I include a random image in my main blog from my other WordPress blog?” Actually it turns out to be very easy.

  1. First, randomimage.php must be installed as described above
  2. Download and unzip randomimage-standalone-1.0.zip (v1.0)
  3. Upload the file randomimage-standalone.php to your plugins directory (alongside randomimage.php):
    /path/to/wordpress/wp-content/plugins
    Note: This is not a separate plugin, it’s simply a supporting file for randomimage.php, therefore there is nothing you need to activate or configure.
  4. On the other blog (or any website, really) where you want the random image to appear, insert the following HTML snippet, replacing example.com/wordpress with the domain name (and directory, if any) where randomimage-standalone.php lives
    <script type="text/javascript" src="http://example.com/wordpress/wp-content/plugins/randomimage-standalone.php"></script>

    Note: for the random image to be appear, JavaScript much be enabled in the browser.

Options

The randomimage function can take eleven optional parameters in between the parentheses. If you specify any parameters this way, all settings made through the configuration interface (shown above) will be ignored. Note: this behavior could be useful if you wanted to have several different random images each with different settings appearing throughout your site.

<?php randomimage(show_post_title,
                  number_of_images,
                  image_attributes,
                  show_alt_caption,
                  image_src_regex,
                  post_type,
                  inter_post_html,
                  category_filter,
                  sort_images_randomly,
                  image_class_match,
                  image_template_html); ?>
  1. show_post_title is a boolean (true or false) that determines whether the title of the original blog post is printed above the image. The default is true.
  2. number_of_images is an integer that determines how many random images the function should return. The default is 1.
  3. image_attributes is a string that gets inserted in the img tag which you can use to set attributes like a standard width or a title (tooltip). The default is an empty string. For example, "width='200'" would reduce the size of the image to be 200 pixels wide (and proportionally high).
  4. show_alt_caption is a boolean (true or false) that determines whether the alt attribute (if there is one) is printed below the image. The default is true. (new in v1.2)
  5. image_src_regex is a string containing a regular expression (without slashes) that matches against the image src attribute. For example, if you only wanted to display jpg’s and png’s, you would use "(\.jpg|\.jpeg|\.png)". If you only wanted to display images from a specific directory called images, you could use "images". This parameter uses the preg_match php function in case-insensitive mode. For more information on constructing regular expressions, check out regular-expressions.info. (new in v1.2)
  6. post_type is a string that allows the following three values: posts for including images from post content only, pages for including images from page content only, and both for including images from posts and pages. The default is posts only. (new in v1.3)
  7. inter_post_html is a string (presumably HTML) that is outputted between subsequent images if number_of_images is greater than 1. The default is <br /><br />. (new in v1.4)
  8. category_filter is a comma-separated value string of numerical category IDs that will limit the plugin to grabbing images from posts assigned to only those categories. The category IDs can be determined by going to Manage > Categories. The default is an empty string which will allow images from posts assigned to any category. (new in v2.1) Even though the name hasn’t changed, this option now includes tags too! (new in v4.1)
  9. sort_images_randomly is a boolean (true or false) that determines whether the image (or images) pulled from the database are displayed randomly or in reverse chronological order. By setting this option to false, the plugin effectively displays recent images, rather than random images. The default is true. (new in v3.0)
  10. image_class_match is a string that you can use to specify a classname that the randomly chosen image’s class attribute must contain. So if you set it to randomimage, only images with class="randomimage" (or even class="left randomimage") will be displayed. (new in v4.0)
  11. image_template_html is a string that acts as a template for displaying the image. With it you can create wrapper HTML around the image, or position the caption on top, or a myriad other things this plugin author hasn’t conceived. Use %1 in the template to represent the title, %2 to represent the image, and %3 to represent the caption. By default, the template is:
    <strong>%1</strong><br />
    %2<br />
    <em>%3</em>

    Note: If image_template_html is specified, it overrides show_post_title and show_alt_caption. (new in v4.0)

If you wanted to use all the options together, you might add the following to your index.php or sidebar.php file:

<?php randomimage(true,
                  1,
                  "width='200'",
                  true,
                  "(\.jpg|\.jpeg|\.png)",
                  "both",
                  "<hr />",
                  "2,5,8",
                  true,
                  "randomimage",
                  "%3<br />%2<br />%1"); ?>

Development Ideas

  • Suppress printing caption if it’s the same as the image filename
  • Check image dimensions to prevent enlarging small images (?)
  • Add option to only display images within a certain width/height range
  • Add a check to make sure image exists
  • Optionally create a thumbnail of configurable dimensions (hard)
  • Store post image info in the postmeta table (hard)

relatedposts

369 comments

name
blog (optional)
comment

This has got me thinking… One of the coolest features of WordPress is that arbitrary key/value pairs can be assigned to posts. Now, the plugin has to run a regex against the content of every post in the database — what if there was a filter action that was triggered at post-time to pull out images and put them into the post metadata table? That would move the processing to the posting stage, but make it easier to get an image when the plugin is called. Or it could just make the whole thing needlessly complicated.

You are so cool.

Hmm, Jackson, I like your thinking about extracting the image metadata when the post is saved, but I’m wary of storing that in a custom table. I guess I could have it store img=>src pairs as post metadata, but I’m also grabbing the alt attribute with my regexes. I’m curious about how the podcasting plugins work for WP, I’ll have to take a peek tomorrow.

I hadn’t considered the alt tag… You could store the whole thing (src, tag, title, etc) as a delimited string (WordPress does this for the Magpie cache), but that adds more processing steps.

Jackson, is there a standard way of triggering a plugin to run when a post is saved?

This is kind of a slippery slope, because I can imagine trying to store the whole post as some sort of DOM tree (cause you never know what kind of plugin I’m gonna think up next).

I wonder if catalogers ever have this kind of internal conflict, given that they know the power and limitations of both searching against metadata and or search against full text.

I think there’s a hook to get your plugin to run when a post is saved, but couldn’t tell you what is off the top of my head.

I know exactly what you mean about the slippery slope. I started going nuts with all the metadata I was tracking for my reviews and had to reign myself in.

Yep, immediately after you left the comment, I rewrote the script to store the image src and alt as post metadata, but I ran into some stumbling blocks getting it to “index” all the posts the first time it runs—and I haven’t had a chance to get back to it.

The code to do so is pretty simple:

add_action('publish_post', 'randomimage_extract_images');

where randomimage_extract_images is a function that does the parsing work everytime the post is updated.

also forgot to mention here, fixed a bug that was preventing pictures other than the first in a post from being displayed.

Help! I installed the code in my site, but it’s displaying both the thumbnails in the sidebar AND the regular sized images which when come up push everything out of the way and bloat the page. What am I doing wrong here?

Nicholas, you need to set the image_attributes parameter so that the image width is constrained to the size of your sidebar:

<?php randomimage(true, 1, "width='205'"); ?>

Thanks a ton! It works now. Great code indeed :D

hey, I was thinking. Is there any way to make it load swf files? I wanted to add both a random image box and a random video box to my page. I tried changing the jpg tags to swf, but that didn’t work. I know the img tags are important, but I’m not sure what to do here.

My code is specific to image tags, and would need to be rewritten or significantly revised to search for swf or other video files and appropriately display/resize them. It’s not a modification I’m planning to make, but anyone who wants to take my code and extend it or use it as a starting point for something else is welcome.

Hi I was wodneirng how I would get your random image plug in to re-size my images so it can fit into my sidebar, I was looking through the comments and found that other justin posted this: , would I add something similiar to that to my sidebar template with the added line of code for the plugin? Sorry but Im such a noob with all this…

^^^^ Please nevermind me, I went ahead and tried it and it works beautifuly thanks guys!

Justin, glad to hear everything worked out.

Hey is there a way to disallow .gifs or have your script only take form a certain directory? I’d like all the images in my /images to show up as random, but not my .gifs in my /moods folder. Thanks for the help!

Justin, ask and ye shall receive. I just updated the randomimage plugin to do what you requested and a little more. With v1.2, you can include a regular expression that will only display images that match the conditions you set.

In order to accomplish what you’re asking for you’d probably want to do something like:

<?php randomimage(true, 1, "width='205'", true, "images"); ?>

In other words it will only display images that have “images” in the src attribute, which sounds like it would exclude any images in the “moods” folder. Hope this helps!

Updated, everything is working perfectly, thanks again.

Great plugins. I try to create before, but i can’t. And i found here. Good job. Thanks for you. I use this plugin on my site.

Hey man this is great stuff. We are using it on our little site and its most helpful. I was wondering though. How do i prevent random image from incorporating a link to the originating post?

I set up a passworded page as an image bank and would like to just pull images from there.

Is this possible? I may be overlooking the obvious.. not sure.

Dusty, there isn’t an option to remove the link back to the originating post, partly because that’s the whole goal of the plugin, to create a link back to some existing content.

However… If you are comfortable modifying the source, you can replace line 131 (in v1.2) of the plugin:

print "<a href='$post_permalink'><img src='$image_src' alt='$image_alt' $image_attributes /></a>";

with this line of code:

print "<img src='$image_src' alt='$image_alt' $image_attributes />";

then you’ll get just an image, sans link.

Your question about a password protected page I didn’t quite understand. But it does beg the question whether the plugin should avoid password protected posts altogether, given that it could publicly expose private content. Which would be bad. Hmm.

thanks man. I really really appreciate it!

No problem. By the way, like I said your questions got me thinking about password protected posts, which I’ve just removed from the equation.

I’ve also added an option to determine whether images are drawn from posts, pages, or both. Enjoy, keep the comments comin’.

Hi Justin :)

I am on the lookout for a random image plugin for wordpress. However I am looking for a script that takes random images as part of the header and displays them onload. I was wondering if your script can just take the images out of special folder instead of searching through the news images.

Tascha, the random image plugin displays images that you’ve already included in your posts (using an <img> tag), and builds a link around the random image it selects linking back to that post. It does not display random images that exist in a specific folder, though that’s an interesting idea—functionally different enough I think to require a different plugin. If this is something you’d like me to work on, leave a comment or send me an email.

So if you’ve already created posts displaying the images that you want to show up in your header, and they all live in a specific folder, let’s call it “special” then you could put something like this in your header.php template file:

<?php randomimage(false,
                  1,
                  "",
                  false,
                  "special"); ?>

Hi Justin, your plugin works great! only that when i put it in my blog, the image comes with a purple border (like those of an image link) and i dont know where to get rid of that in my css… im a complete dummy, could u show me which part governs image link borders? like in your blog, the white borders of pics look great.

Eric, to remove the borders around images add the following to your CSS file:

a img {
    border: none;
}

Is there anyway to make the images line up horizontally rather than vertically when you have more than one image showing?

and also, is there anyway to stop duplicate images when you are showing more than one image?

Louise, great questions.

Actually no, you can’t currently display the images horizontally because the plugin adds a line break between each successive image. Also the post title and image caption add line breaks.

One possible workaround: If you set show_post_title and show_alt_caption to false and then comment out line 173 of v1.3:

//print "<br />\n";

by adding two slashes to the beginning of the line, I believe you should be able to display images horizontally. Of course it probably should be easier to do this—with a configurable header and footer around each image.

You’ll probably also want to set an image_attribute like "height='200'" so they all have a common height.

Regarding duplicate images, if the image is included twice in a post or in more than one post (a scenario I hadn’t accounted for) then you’re right, there’s a chance duplicate images will show up. This is a bug. I’ll fix it when I get a chance.

Louise, I’ve updated the plugin to solve both of the issues you brought up.

First of all, it should no longer display duplicate images. Thanks for noticing that bug.

You can also now configure a new inter_post_html parameter to control what gets outputted between images. If you set this to be an empty string ("") and you set show_post_title and show_alt_caption to false, multiple images will be displayed horizontally.

Putting everything together, you’d probably want to insert something like this into your template:

<?php randomimage(false, 8, "height='50'", false, "", "", ""); ?>

[…] I added Justinsomnia’s Random Image plugin today, which is amusing me greatly. […]

I love this plugin! I love this plugin! I love this plugin! I love this plugin!

Sory, I try to use a directory to print random images but I get nothing
My wordpress is à the root
home/procom/domains/licencepro-communication.com/public_html/

and I want to use picture in “images”

Interesting usage: Berlin Real Estate Investments appears to be using the Random Image Plugin to select header image graphics from a contact us page.

Hi Justin, great plugin, but I have a little question, if possible…
I have a photoblog in another directory on my site ( 2 wordpress installations ) and I wish that on my primary blog where your plugin is installed that the random image is linked to the same image of my photoblog.

Is possible to realize this ?
2 wordpress = 2 databases !!!
please drop me a line about this. btw great work.

Great work !!! I love this plug !!

djmitch, I was hoping there’d be an easy way to do this, possibly by replacing global $wpdb; with a php include() to your other WP install’s wp-config.php file, but this didn’t seem to work for me.

Furthermore, I rely on the get_permalink() function for generating a posts permalink which would be specific to the blog you add the plugin to. This code would need to be rewritten or replicated.

So in the absense of my knowing how to instantiate another WordPress install from within WordPress, it seems like you’d have to hack on the plugin a fair bit to get it to work—but it should be possible.

Hi Justin,
infinite thanks for your fast and detailed answer.
Really appreciate.
I will try something, and will back here to inform you if I will realize my wishes….

thank you again mate…

Dear Justin,
I currently need a WP plugin that is able to show random images in my front page. Yours is not quite what I’m looking for, however, it gives me an idea to create a new plugin with similar ability.
Is it okay for me to use your code as an inspiration? As usual, I’ll put your name on the new plugin. How about it? Thanks before.

Matt, go right ahead, the code is released under a GPL license. And feel free to come back and add a link to your random image plugin if you release it as such.

Hi Justin, just a clarification: if one chooses the ‘only images from a specified directory’, is the parameter: “images”, and where is the ‘images’ directory, in wp-content? Thanks for advice

Martin, the image_src_regex parameter matches against whatever the src attribute of your image tags contain.

As far as where you upload your images to, I’m not sure. You can tell by looking at the src attribute of your image tags.

Do you use the integrated WordPress image uploader? If so, you probably don’t need to bother with that parameter, you can just leave it as an empty string.

This parameter is more useful if you have some images in an icons directory, and some in a photos directory, but you only want the random image plugin to look at the photos, so the parameter would be “photos”.

Am interested in associating a specific URL with each random image. The idea is to create an easy random advertisement generator for local advertisers. I assume this would require registering the image with WordPress (like the Random Header plugin does) and an admin interface. Any idea if this is on the burner? Or if there’s another plugin that would do the job? Thanks. This looks pretty cool as is and I’ll probably use it for my personal blog’s sidebar. Thanks for sharing.

Is there a way of specifying that images come from only one category?

Hi, is there anyway for the script get the random pics from a specified location /directory ?
Thanks in advance, great plugin by the way.
[]

Sergio, nope my plugin is intended to grab a random image that already exists in one of your posts and then create a link back to that post. Perhaps Matt Mullenweg’s A Rotator Apart is more what you’re looking for.

You thought about turning this into a Widget?

You mean WordPress Widgets? Nope hadn’t thought of it till you brought it up. Should I? Are you using a Widget-compatible theme?

Yes you should, and yes I am!

I tried to do it myself but broke my site until I deleted my little widget attempt! *blush*)

I am runing WP2.01, and would love to use your plugin, but I am running into an odd problem. In the config interface, and random image appears, but on the blog, the same image appears over and over again.

Also, it only appears in FF. It doesn’t appear at all in IE or Opera.

oops..I figured out the only apears in FF thing..it is only appearing for users that are logged in. Is there a way to change that?

Okay…I panicked. I can admit that.

I figured out the problem. I am running wp-cache, and believe it or not, it was caching.

I still can’t figure out how to exclude the sidebar, but I am sure that I will soon.

Sorry for the multi-posts, but I guess it is good info in the long run!

MilitantPlatypus, it appears to be working on my end, at least reloading your homepage brings up a new image in the upper-right hand corner each time. Feel free to post again with whatever you figured out to get this to work.

Hi Justin,
I’ve modified your plugin so it will get images not from the posts but from a certain folder on the server that will link back to the original image. The problem is that I had just registered my new domain so it won’t be available until next thursday or friday. But if you would like to see it in action please check out this website that I redesigned: http://www.clgi.or.id/aceh/

Hi Justin,
This looks like it might be the plugin of my dreams. I want to have a random collection of book covers in my sidebar which link to archived posts about that book. So far so good. My question: is it possible to either a) set this up so that the images don’t need to be in the actual posts but can be stored elsewhere, or b) have the images in the post as you intended, but limit the selection to just one category?

I’ll go back to sl-ooo-wly rereading your instructions now ;)

Laura

Widget! Widget! Widget! *hehe*

Hi Justin,
Great feature. How to modify code so it will get images not from the posts but from a certain folder on the server that will link back to the original image.

Taru, the whole point of this plugin is to display a random image from one of your posts with a link back to that post.

If you want to display a random image from specific folder, I’d Google around for a plugin that does that. One example is Matt Mullenweg’s A Rotater Apart or Kamiel Martinet’s Wordpress random header plugin.

Catherine and Laura, I’ve updated the plugin (now v2.1) so that you can now display random images from posts associated with specific categories.

Jason, I also added better instructions to the configuration interface (which I think is what you were asking for).

Hi,

I was wondering how to get a border around the random image in my sidebar. I hope someone here can help me :)

koen, set the optional attributes setting to something like:

style="border:1px solid black;"

Works, thanks a lot :). Great plugin!

This is a fantastic plug-in and I’m using it in two different spots on my homepage, linking to two different categories. I’d like to use it to pull random images from just one page. Is that possible? I tried putting “pages” instead of “posts” with the page ID where category ID goes, but it didn’t work.
In any case, thanks so much for a very nifty plug-in.

Can i use this plugins for my 2.0.3 wordpress?

rionya, yes it should work in Wordpress 2.0.3. Let me know if you have any problems.

i use custom field plugins. i add “images” field. Can i still use this plugins ?

sory, late. the content of “images” field is like: http://www.example.com/pic.jpg

rionya, this plugin only looks for image URLs in the content of your post, it does not look for image URLs in custom fields.

However, it would be fairly trivial to modify the plugin to do so (in fact it would simplify the plugin greatly as much of the code concerns parsing the post content for the image src and alt attributes).

Let me know if this is something you’d be interested in having done.

so, anybody can help me ??

rionya, I just offered to help you. If you’d like to chat more, feel free to send me an email.

This is amazing, do you have any tips for how to display this on a website rather than a wordpress blog?

Jaree, I don’t see why not. This particular plugin is designed for WordPress in particular, but the concepts contained within (searching through a database for content containing images, extracting the image src attributes, generating an image tag that links back to the source content) are all easily replicable in another content management context.

Very cool and so easy to implement. Love this idea. Thanks a lot! Unlike so many other plug-ins I’ve tried, I really did just activate it and it worked. Very slick. Good job.

Brock, thanks leaving a comment, glad you found it easy to use. Great use of the plugin in your header.

Wow, this is *precisely* what my daughter described she wanted in her blog I created, except she wanted the last [n] recent photos (a different purpose than yours). Do you happen to know a snippet to replace your random with recent? I’m thrilled to find this, thanks!

echo, done. I updated the plugin to v3.0 and added a configuration option that allows you to turn off the random image order to effectively display the “n” most recent images with links back to the posts they came from. Let me know if this works.

Wow, Justin, you rock!
I’ll report back with a link after upgrading. It’s going to look (+ work) fabulously!
As an aside, I’m wondering why on your site, you don’t seem to specify the column’s width on your images, so their height is proportionately scaled, as I understood your instructions to mean.

echo, on my site, I use the “optional attributes” field to set id="random-image" on the image (since I only ever display one random image).

Then in my site’s stylesheet I restrict the width (and the height proportionally, by default) using the following CSS:

#random-image {
    width: 167px;
}

If you actually look at my CSS, you’ll see that I jump through a few more hoops to allow the image to scale down when the page’s width shrinks in browsers that support max-width (e.g. Firefox, Opera, etc). But the end result is the same. For most people I wager, setting the width on the image itself is a far more accessible technique.

Oh, I see in FF it’s working as intended, but what I was reporting is unfortunately Safari is displaying the images original height, so it’s not proportional, yuck! I hope there’s a solution, I’d be glad to test in Safari if you don’t have access to a Mac.

Ah yes, for some reason this is a place where Safari differs from both Firefox and IE. I’m not sure why. Unfortunately I don’t have a Mac to test on nearby, but I’d suggest a few strategies:

* try a percentage or em width
* try a setting a max-width

great plugin, thanks!

Nice plugin !

Just what I was looking for.

Had to add,

a:link img,
a:visited img,
a:active img {
    border-color: #a29844;
}

a:hover img {
    border-color: #ffffff;
}

to my stylesheet to change the color of the ugly blue border around the image in Internet Explorer 6.

Hey thanks for the plugin.

Will try and let u know if i have any problem.
Hope you help.

Hey isn’t there any perfectsize for the image?

Like if in the post i place 300 x 300 image, can that be resized to 100 x 100 image in the random images section.

I got it.

Thanks for the plugin once again.
Check it in my site and tell me whether it is correct or not.

hi, nice work but i have problem with the lang.
some characters like i, ş, ü, ç, etc. are shown like that sayısı
what do u offer for it ?

I’m faced with following error when I activate the plugin :( what should I Do?

SELECT wp_posts.ID, wp_posts.post_title, wp_posts.post_content FROM wp_posts WHERE post_content LIKE '%

Paing, that error is indeed strange. Could you have possibly modified the source of the plugin, (maybe by accident)? Was the error any longer that what you included above?

You could help debug the problem by replacing line 368 of randomimage.php:

$resultset = mysql_query($sql) or die($sql);

with this:

$resultset = mysql_query($sql) or die(mysql_error() . " " . $sql);

Then reactivate the plugin, copy the full contents of the error message you see, and paste it into a comment here. Thanks!

Hi Justin, would it be as simple as going through the plugin file and changing the word randomimage to randomimage2 then saving the file as randomimage2.php if I wanted to impliment another seperate random image plugin on the same WP site?

BTW great plugin!

Tom, if you want to use the random image plugin twice on the same site but with different configurations, you shouldn’t need to make a copy of the plugin unless you intend to modify the source code.

Instead you can use function parameters to customize the output (described under Options at the bottom) for each instance that a random image is displayed which will override the defaults in the configuration interface.

Thanks. Renaming the word randomimage to randomimage2 works great. I can then control the options via the admin. Which is handy if I decide to us this plugin for a client. Thanks again.

I’ve bookmarked your site in my to paypal section if I ever make any money :)

Tom, good point. Glad that solution worked out for you.

Hi, Good Morning. Is there any way to have a Random image in a page or post??

Sorted it with runPHP :)

Tom, I hadn’t tried embedding a random image inside a post/page before so I’m glad to hear runphp made it possible. Feel free to include a link to the site you’re working on when it all comes out.

Best. Plugin. Ever.

Thanks so much for this, it works great and I love it. I sit there refreshing for 10+ minutes at a time, just to see what will come up next. It’s a terrific way to encourage readers into your blog’s archive. Great work!

Burbanked, wow, geez, thanks for the praise.

[…] I searched for a plugin that would help me do the job. The first one I found was the Random Image Plugin. This plugin randomly selects an image attached to a post using WordPress’s built-in upload feature and displays that image. This is not what I wanted to do. […]

Hi, thanks for this amazing plugin, exactly what I needed!
My only problem now after installing it, is that the jpgs in my posts are pretty large. On my front page I want to appear 6 small random jpgs. They now take some time to load. I was wondering: Wordpress automatically uploads thumbnails for each image. What would be neat is if I could make your plugin pick the thumbnails instead of the actual jpgs. The thumbnails are systematically named as: name.thumbnail.jpg
My php skills are not enough to change your plugin, to add the .thumbnail between the pictures name and the extension. Could you help?
thanks!

Remco, sure no problem. Just open v3.0 of randomimage.php in a text editor, scroll down to line 431, right below this line:

$image_srcs[] = $image_src;

and paste this line of code:

$image_src = preg_replace("/(jpg|jpeg|gif|png)$/i", "thumbnail.$1", $image_src);

That will take the src url for every image that ends with jpg, jpeg, gif, or png and replace it with thumbnail.jpg, thumbnail.jpeg, etc. Be aware that this plugin is nondiscriminating—it doesn’t check to make sure the thumbnail image actually exists. If it doesn’t, you’ll get a broken image.

Hi Justin, Thanks a lot for your fast help! I added the code and it works perfectly. This is why I love wordpress, a lot of nice people around willing to help out. Thanks again.

Hiya, I really like your plugin and will be using it on the site under the sidebar as soon as i get chance to edit the file so it displays the thumbnails instead.

However I suddenly realised it could be usefull for one of my other projects. I am trying to create a gallery of all the photos uploaded by the authors on the site
Heres what ive got so far:
http://www.teampyranha.com/gallery2.php

Questions:
1 if i call random image multiple times will it cycle through the images without reapeating them.
2 if i call random image more times that there are photos what will happen? if there is a problem is there any way to prevent it?

lozbrown, if you set the number of images attribute to some large number, e.g.

<?php randomimage(false, 10000, "", false); ?>

…the script will just run until it exhausts all possible posts that have images, and it should never show a duplicate (based on the src attribute).

I didn’t realize WordPress had started automatically creating image thumbnails. I’ll look into adding a configuration option to grab thumbnails only so folks don’t have to hack the plugin source as I suggested to remco above.

Firstly Thanks for your speedy and Really helpful reply!

Can you suggest any way to arrange them in a grid?

To be really cheeky I’m going to suggest some more usefull configuration options, such as:
Drop down box labeled: link images to
with options:
-attachment page
-originating post
-author
I’m sure you can work out what each of those is supposed to do.

Another usefull option if possible would be to be able to have random images only from the current author , this would be enable you to put a few random images by each author on there author page.

But then there all probably very specific features that probable only I would be intrested in.

lozbrown, cheeky suggestions are always welcome, I’ll see what I can do. I’m not sure what you mean by “attachment page” though. Can you give me an example URL?

As far as arranging the images in a grid, the only way to do this cleanly (as the plugin is currently written) is by floating the images to the left and ensuring that they all have a constant height. You’ll also need to set the inter_post_html attribute to an empty string to prevent the default behavior of double newlines (<br /><br />) from showing up between the images:

<?php randomimage(false, 10000, "align='left' height='100'", false, "", "both", ""); ?>

If you want the images to show up in a table with a set number if images per row, then we’ll have to do some plugin hacking.

Btw, the pictures on your site are unbelievable. I’ve done a little seakayaking, but nothing like that.

Hiya

Firstly attachment.php is part of some themes and looks something like this http://www.teampyranha.com/?attachment_id=115

It chooses which photo to display simply by the url encoding, I think the 115 in this example relates to id of the dummy post that stores the info about the attachment.

Oviously it would link to the page relevant to the photo you have clicked on.

The gallery may require a different plugin based on similar code. I will have a look into it but my expertease with this kind of stuff is small to none.

With reguards the photos unfortunatly none of them are mine, I’m not that good I just run the site, the guys who post on it are an elite bunch who are sponsored by one of the worlds biggest canoe kayak manufacturers.

Btw:
I tried using the random image thing with an arbitary large number with the following results:
http://www.teampyranha.com/photos/

This some how only gives 15 results rather all.

Also you get a random mix of thumbnails and full size images depending on whats been used in the post.

Also the code you suggested to remco will be problematic due to the fact if the image is already a thumbnail it such as image.thumbnail.jpg it will make it image.thumbnail.thumbnail.jpg which will probably be a broken image.

[…] Eklenti Adı : Random images (Her Açılışta Farklı Resim) İşlevi : Sitede o zamana kadar yazmış olduğunuz yazılardaki resimlerden istediğiniz kadarını, sitenin istediğiniz yerinde rastgele bir şekilde gösterebilmenizi sağlar Örnek : http://www.yakuter.com Kaynak : http://justinsomnia.org/2005/09/random-image-plugin-for-wordpress/ […]

This is a great plugin for a site I am doing for friend. I too wish it was easier to do a grid. Currently just resizing thumbnails… It would be great to crop them (like flickr plugins I’ve seen) so they dont squash.

Thanks again!

oh and the site is here:
http://accuracyandaesthetics.com/

and I a