Random header images for WordPress
I’ve finally migrated my family’s site over to the latest version of WordPress, and installed pretty much the same batch of plug-ins and widgets as I use here. However, I wanted something else, too–a randomly-selected image for the header of the site that changes each time the page is loaded, as seen in these four sample pictures:

(The header images are just sections I’ve snipped out of photos we’ve taken, with an artsy Photoshop filter of some sort applied.)
I searched the web, and there are a few plug-ins that offer this ability, but they came either too feature-rich, or required some additional JavaScript to work properly. I wanted the most simple, basic, and functional header image rotation solution I could find…so I wrote my own, which required all of two lines of code. I’m posting it here so that (a) I remember how I did it, and (b) in case anyone else wants a simple solution, they’ll be able to find it with some help from Google (our family’s site is access restricted, so posting it there wouldn’t do much good…and it would confuse my relatives, who are used to only seeing pictures of our kids there!)
My solution is based on having a theme that loads the header image in the theme’s CSS file–this is the norm among the themes I’ve seen. In your theme’s CSS file, you’ll probably find a declaration like this one:
#header {
background: url(img/header_pic.jpg) no-repeat center;
color: #FFFFFF;
height: 105px;
width: 760px;
margin: 0;
padding:0;
}
My solution is to simply replace the background: url bit of that declaration with a new definition. So here’s how I did it.
- Create a folder of header images within your theme’s img folder. I called my folder headerpix. Within that folder, name each image sequentially. In my case, I named the files header1.jpg, header2.jpg, etc.
- Open up your theme’s CSS file and comment out (surround it with /* and */) the background: url definition (or just delete it).
- Open up your theme’s header.php file, and find the line that loads your stylesheet; it should start something like this:
<link rel="stylesheet" href="<?php bloginfo('stylesheet...The trick here is that once your stylesheet has loaded, you can refine it by defining new styles directly in the header.php file.
- Below the line that calls you stylesheet, insert the following code:
<?php // Replace 26 with the number of images in your folder! $imgnum = rand(1,26); ?> <style type="text/css"> #header { background: url(<?php bloginfo('template_url');?>/img/headerpix/header<?php echo $imgnum; ?>.jpg) no-repeat center; } </style>If you didn’t name your folder headerpix, or your images header#.jpg, then modify the background line above as appropriate (and make sure you change the $imgnum line to reflect your image count).
That’s it–you’re done. Your site’s header image should now change randomly each time the page is loaded. There’s a ton more that could be done with this code, obviously (it should be possible to get the image count from the folder itself, instead of hard coding it), but it works well for me–and my relatives love the rotating collection of images that now appear atop the page each time they load it.


July 27th, 2007 at 9:51 pm
Thnx for the info. I tried but it’s not working maybe because I am not a programmer and may have messed up. Keep it up.
August 24th, 2007 at 5:41 am
Thanks for the information on how to do this.
Works like a charme !
September 22nd, 2007 at 8:11 pm
I just get a blank box… any chance you can help me tweak this?
Thanks!
September 23rd, 2007 at 8:20 am
If you want to cut-n-paste and email me the bits of code, sure. Email is robg at this domain.
-rob.
September 30th, 2007 at 4:33 am
I really like how you did this but unfortunately I can’t get it to work on my site. I think my code is a bit different and I’m not sure which bits are affecting the outcome. I’ll keep trying because I really like the result.
December 8th, 2007 at 11:31 am
Many thanks for posting your code Rob. This is exactly what I was looking for – a simple, straightforward random image header without any unnecessary extras or plugins etc. It works great!
May 20th, 2008 at 8:16 pm
Thank you ever so much for this, Rob. I fought like a newbie for hours with some other folks’ version, but yours was easy to follow, and it works like a champ.
Thanks again!
July 25th, 2008 at 9:50 pm
This is a great piece of code, but is there any way for it to work w/o disabling WP-Cache? I can only get the images to rotate when I clear the cache or just turn off caching. Anybody else had similar problems?
October 1st, 2008 at 12:27 pm
Gread post! May i share it on my blog?
October 6th, 2008 at 6:25 am
Avita: Sure.
Jonathan: Not sure :) … I don’t use wp-cache, but it makes sense that it would stop working with cache enabled. There may be some way to say ‘don’t cache these images,’ but I’m not sure what it might be.
-rob.
January 2nd, 2009 at 2:59 pm
This worked perfectly, thanks! I’ve been searching for simple and straightforward code for a background image rotator for a couple weeks now and this is the only one that’s worked for me.
February 28th, 2009 at 10:52 pm
YES!!! Thank you so much. I have been looking for an easy way to do this for a while, and this works like a charm. You Rock!!
May 21st, 2009 at 4:52 am
Nice little tip, works great.
Thanks again,
Darren.
June 8th, 2009 at 7:25 am
Thank you! This was very helpful. :-)