Friday, August 8th, 2008...3:34 pm

Quick and Dirty Random Thumbnails

Jump to Comments

I’m posting this for a friend who asked about the gallery images in the sidebar of this site. Refresh the page, and you get 3 new random thumbnails. It’s fun. I could do it for hours, stumbling on pictures I haven’t seen in ages. How’d I do it? A simple script dropped into WordPress and a little-known feature of the Gallery2 application.


After searching extensively through the WordPress image plugins, I decided nothing would do exactly what I wanted. I’m not yet ready for a flashy slideshow plugin or a heavy-duty image management system that runs inside of WordPress. For now, my image gallery at http://www.silkworms.com/gallery/ suits me just fine. I have nearly 7,000 photos uploaded, with many thousands more ready to upload. Why would I move my images? So, using the WordPress Admin Dashboard, I dropped this bit of PHP into a div in sidebar.php.

[code lang=”php”]

Moved Permanently

The document has moved here.



Moved Permanently

The document has moved here.



Moved Permanently

The document has moved here.


[/code]

Piece of Cake!

Definitely use this sort of external call sparingly, since the cURL requests here are not threaded. If the host you’re calling is down or limping along, those 5 second timeouts could stack up and make your blog unbearable slow.

One caveat.  Depending on your hosting arrangement, mod_security setup, and .htaccess files, making this change through the WordPress web interface might fail. The “exec” in this snippet is often filtered out for security reasons. You might have to modify the wp-content/themes/$theme_name/sidebar.php file directly on the server, rather than through the web interface.

If allow_url_fopen is enabled on your server, which of course would be a bad security practice, you could accomplish this script in even fewer lines:

[code lang=”php”]

[/code]

If this last snippet works on your server, I’d suggest disabling allow_url_fopen in the php.ini file immediately. This is a very frequently exploited PHP vulnerability.

Leave a Reply