Header-Rotator

A few late night tweaks to the header image logic: I’ve created as simple a plugin as possible that lets me have three of my own images randomly appear in the heading. Based on the TwentyTen Header Rotator plugin, I ripped out all the code other than the logic to hook into the WordPress framework and the last line to pick an image at random. For my first pass, I hard-coded threefour images (a full moon on a snowy evening, a frosty window and a full batch of cherries, all taken at home) to load up the array. Got it working. That’s the first sanity check. It will be followed with admin pages, setting pages, the ability to upload and resize your own images, etc.

I tried to keep it as close to “the simplest thing that would work” for a first pass, but I left a little complexity in there: the list of images is an array of one-element arrays because the main theme uses something similar where you can store more information about each image, such as a thumbnail location. I figured I’d eventually need to add that back in there, so I left the structure in place. Here’s the core code that gets it to work:


if(!is_admin()) add_filter('theme_mod_header_image','trhr_rotate');
 
function trhr_rotate(){
$imagelist = array(
0 => array('url' => home_url('/wp-content/uploads/2011/03/cropped-DSCN1285.jpg')),
1 => array('url' => home_url('/wp-content/uploads/2011/03/cropped-DSCN1288.jpg')),
2 => array('url' => home_url('/wp-content/uploads/2011/02/cropped-SnowMoon.jpg')),
3 => array('url' => home_url('/wp-content/uploads/2010/07/cherries.jpg')),
);
return $imagelist[rand(0,count($imagelist)-1)]['url'];
}

Thanks to the Seacoast WordPress Developers group for the inspiration, and also some cool ideas from Smashing Magazine’s “Ten Things Every WordPress Plugin Developer Should Know

Powered by WordPress. Designed by Woo Themes

This work by Ted Roche is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States.