Jane Austen’s Fight Club: for some reason I thought of Eric while watching this.

Random images in WordPress

Putting random images in the sidebar of a standard Movable Type blog is relatively easy, because Movable Type generates static pages that the web server then dishes out. Tell your web server to run the pages through PHP before sending them out, and include a little bit of code to stick in random image URLs—the PHP will survive intact through Movable Type, and your server will take care of the images before sending the page to the browser.

This is even easier in WordPress, because—by default—pages are dynamically generated (using PHP) when the browser asks for them. Add your image rotation code in, and WordPress will take care of the rest.

The problem occurs when you want to add page caching to WordPress. Generating a page each time a browser asks for it is rather wasteful; I can’t count the number of times that Daring Fireball has crashed a server by linking to a WordPress blog it was hosting. The solution to that is saving those generated pages, and then using that saved copy when some other browser asks for that page.

Which is all well and good, until you realize that the cache has also cached a specific (albeit random!) set of images for your sidebar. Everybody will see the same images when visiting a specific page (say, this front page), and they will continue to see those images until the cache expires and the page is re-cached.

W3 Total Cache, the caching plugin that I’m using, has a workaround: it can cache parts of a page, so long as you use the “Disk (basic)” page cache method. You surround the parts you don’t want to cache with <!--mfunc--> and <!--/mfunc-->, and it’ll cache the rest. Notably, you omit the usual <?php and ?> tags when using this “mfunc” notation. My code looks something like this:

$image_path = 'rotating_pics/';
$temp = array_merge(glob($image_path . '*.jpg'), glob($image_path . '*.png'), glob($image_path . '*.gif'));
$pic_list = $temp?$temp:array();

$top_pic = $pic_list[0];
$mid_pic = $pic_list[1];
$end_pic = $pic_list[2];

echo '<div id="background_pic_1" class="side_pics" style="background-image:url(' . $top_pic .')"> </div>';
echo '<div id="background_pic_2" class="side_pics" style="background-image:url(' . $mid_pic .')"> </div>';
echo '<div id="background_pic_3" class="side_pics" style="background-image:url(' . $end_pic .')"> </div>';

I’ve gotten lazy, and am now using the PHP function glob() to figure out what pictures are available for use—rather than hardcoding the file names into a script. The server I’m on doesn’t support it, but the GLOB_BRACE flag could combine the three calls to glob() into a single one. (I’m also using shuffle() over array_rand(), as the latter apparently isn’t exactly random when it comes to ordering the selected elements.)

And that’s pretty much the fruit of half a day’s poking around on my week off. It may be a little bitter for me (really… my week off), but may be useful for someone else.

You Are Not So Smart: this “celebration of self delusion” is in the running for my favorite blog, ever. Well-written, interesting, and informative… everything my blog is not.

*Phoenix Down*

It took me getting sick on my week off (cry) to sit down and finally do it, but Ye Olde Blogge has been restored to its previous grandeur. (There are some new pictures in the side rotation, too… very exciting.) It turns out that WordPress is pretty OK, until you want to do something that WordPress doesn’t want to do.

My designs butted heads with WordPress in two major areas: combining page caching (to cover the 0.000000001% chance that a major site ever links here) with random side images, and monthly page navigation. Getting things set up just so took something like three full days of hacking (…hacking while sick, mind you); I’ll have to write up something about that for the search engines to find, to hopefully save others some trouble in the future.

Now to see if I actually post anything of value, since I’ve spent so much time getting this place set up again….

Well, it’s a start

As you can tell, I’ve installed WordPress. It wasn’t exactly a five minute install, but it was far easier than I had feared. (Granted, it helped tremendously that I’ve already figured out things like enabling PHP extensions and .htaccess files.) WordPress itself seems to be pretty slick, and the admin interface is soooo much faster than Movable Type’s.

As I warned, the blog will be sporting the generic WP interface for the time being. There are all sorts of rough edges, I’m sure; what’s stood out to me, initially, is that inline pictures work on the front page, but not on individual pages. That’s certainly because of my old-school habit of hardcoding references to my images, rather than using the CMS’ media capabilities… ah well, I’ll get things sorted out eventually.

Life has an annoying tendency of requiring you to destroy things in the process of improving them. (I’m reminded of Evangelion’s the fate of destruction is also the joy of rebirth.) We’re just in the middle of that whole destruction/rebirth process.

Let’s do this

I’m going to start the process of migrating to WordPress here (though I’m going to begin by taking a break to watch House), so things may be unusually interesting for the next {period of time | couple hours <= period of time <= couple weeks}.

(I am using math notation in a midlife crisis sort of way, to try and prove to myself that I haven’t lost all my math skillz.)

Expect this place to look unusually generic, until I wrap my head around WordPress’ templating system. On the off chance that inline pictures survive, I have included a random anime picture below, so you know you’re in the right place:

I don’t know if the RSS feeds will be in the same place when I’m done, so if you’re using that to keep tabs of my sporadic updates (I would be using RSS, too, if I were you) you may want to check back at the site proper and re-add the feed, if you don’t get any new posts after this one. You could make a snarky comment about how that wouldn’t be any different than normal, like I was going to, but then you might think about it a bit more and correctly surmise that I’ll post something once WordPress is up and running.

WordPress, ho!

So I heard you liek mudkipz that Six Apart is getting rolled into a new corporation called SAY Media, and that SAY Media* issues press releases full of gibberish. (Six Apart is the main force behind Movable Type, the blog software I currently run.)

*It is a mystery why I bother respecting SAY Media’s inane capitalization, but don’t respect Sony’s attempt to make sure that PLAYSTATION 3 is always all-caps.

As should be blindingly obvious by now, I’m not in this to grow and monetize [my] audience. That phrase is a baseball bat to my gut, and the moment I read it was the moment I knew that my time with Movable Type was ending. Nowadays all the cool self-hosting kids are running WordPress, so I figure that’s where I’ll turn my attention when I get some free time.

(Yes, I do intend to start posting here again. Eventually. Soon. Tomorrow, I swear!)

powered by wordpress