Dynamic Page Graphics in WordPress

Recently, while developing a WordPress theme for a client, I devised a handy trick to use different headline graphics for pages (it should work for posts too) based on the page title. The basis of the trick is to name your headline graphics the same as the pages they are to show up on (ex. about.png for the About page). Once you do this, you simply need to insert the following bits of code into your theme’s page template:

Part 1: Add this to the beginning of your template (just after the line that starts with “<?php get_header(); ?>”):

NOTE: Modified this to actually, you know, WORK! 🙂
If you tried it before, and it didn’t do what you expected, change the code at the top of your template to this new version.

	global $post;
	$title = $post->post_name;
	$title = strtolower($title);
	$titlepic = $title.".png";

For those who don’t read PHP, what this block does is gets the title of the current page, stores it in a variable, converts it to lowercase, and then adds “.png” (changes this if you use gifs or jpgs) to the page title to create the image’s file name.

Part 2: Add this where you want the graphic to appear:

<img src="<?php bloginfo('template_directory'); ?>/images/<?php echo $titlepic ?>" alt="<?php $title ?>" />

This is a standard XHTML image tag. Change the directory to suit your needs, but leave the “<?php echo $titlepic ?>” bit as this is what does the magic. The alt parameter could likewise be changed, but it does need to be there for the XHTML to validate.

So there you have it. A painless method to get dynamic graphics for your blog, based on your page titles.

CTC Updated, Version 4.5 Released!

Just a quick note to let everyone know that I have bumped everyone’s favorite tag cloud plugin for WordPress 2.3+ to version 4.5. What’s new? The biggest change is the addition of an Options page for configuring the plugin when used as a template tag. As always, you can see the full changelog, get the new version, and leave comments from the CTC page.

TextMate WordPress Development Bundle

Just a quick note to release my newest WordPress-related download, a bundle for the excellent OS X text editor TextMate for doing WordPress development. The bundle includes template for common WP theme files, updated snippets for newer versions of WP, and various other time-savers. Head over to the Bundle page to check it out and download it.

Reciprocity WordPress Theme

After switching my hosting and theme earlier this month, I decided to update and release my original blog theme to the general public. Head on over to the Reciprocity WordPress Theme page to take a peek and download the theme.…

After switching my hosting and theme earlier this month, I decided to update and release my original blog theme to the general public. Head on over to the Reciprocity WordPress Theme page to take a peek and download the theme.

Fun With Shell Scripts: autons

Being a freelance web developer, there are many times where I’m moving domains for clients (or more recently, myself), and need to know when the DNS entries change to point at the new version of a site. I could just keep submitting an nslookup request manually, but that requires me to stop whatever it is I’m doing, switch to my terminal app, and execute the command. After doing a few sites this way, I decided that there had to be a better way…and there is. I came up with the following bash script to automate this process.

#! /bin/bash
while [ 1 ]
echo Attempt $count
nslookup $1
sleep $2
count=`expr $count + 1`

Yes, it’s just that simple. As you can see, the script takes 2 parameters, the domain name to check, and a time (in seconds) to pause between each check. I don’t know if there’s a rule about the amount of time between requests for nslookup, but I usually don’t use anything lower than 5 minutes (300 seconds).

To use, just paste the code above into a file, save it (I named mine “autons”), and give it execute permissions (“chmod +x autons”). To test a site, use something like the following: “autons yahoo.com 300”. This would check the DNS address for yahoo.com every 5 minutes.

CTC – Version 4.1 Released

Just a quick release to add a feature and fix one. The new feature is the ability to randomly sort the tag cloud. This option will show a completely different order of the tags to each visitor to your site. Not really earth-shattering, but it was asked for, so there it is.

The fix is in relation to the use of categories in the cloud. Due to laziness on my part, I released version 4.0 before thoroughly testing this aspect of the plugin. The problem was, your category links were being treated as tags, and therefore not getting a valid link created for them. This has now been fixed.

As always, you can find the download and comment on the plugin on the CTC page.

CTC – Version 4.0 Released

Hot on the heels of version 3.1 (and the very minor 3.2) comes the all new version 4.0 of my configurable tag cloud widget for WordPress 2.3+. What makes it new, you ask? Well, how about the ability to include your WordPress categories in your tag cloud? It’s a feature that people have been (erroneously) assuming CTC supports for a while, but I never could get it to work…well, now I have. It’s as simple as adding another parameter to your template tag, or clicking a radio button in the widget admin. Give it a try, I’m sure you’ll like it!

Also new in this release is some tweaks to the color functions. Prior to this version, if you wanted your tag cloud links to use the same color as the rest of the links in your site or sidebar, you had to manually enter the color definition in both the min and max color fields or parameters. No more. Now, if you leave both colors blank, your tags will be the same color as the rest of your links. If you only use one of the two fields, your tag links will all be that color.

As always, you can find more info and the download (and leave your comments) on the CTC page.

Off-site Backups and Amazon S3

After losing two hard drives in rapid succession, I figured it was time to start giving some serious thought to my backup plan. I have been rsyncing my documents, pictures, and active projects folders to my local Debian server for a while now so neither drive loss was that critical, but it drove home the fact that while I like to think my systems are bulletproof, there’s no such thing. At that point, I decided I needed a robust off-site backup plan.

I started looking at the various free services that were available. I tried a few of them, but the biggest drawback was most locked you into a proprietary application for performing backups. The notable exception here was Omnidrive, which I used for a while and liked, until they upgraded their software and it stopped working on OS X. The other major drawback to the free services was the available space. Most providers only give you a gigabyte or two for free, and then charge you beyond that.

Since I was going to have to pay to get the amount of space I needed, I started looking at real offsite backup providers. I found several that offered what I was looking for, but none were within my price range. While researching the various options, I came across Rob Rohan’s article about how he used Amazon’s Simple Storage Service (S3 for short) as a back-end for a server backup plan.

After looking at how much (roughly) it was going to cost per month, I started using S3 last month as my primary off-site backup destination. I couldn’t be more pleased with the outcome at this point. I followed Rob’s technique for the most part, but skipping the database backup stuff since I don’t need it (but I might start doing it, you know, just in case). The first backup took forever of course (we’re talking almost 5 gigs being pushed up my cable connection), but the subsequent sync operations fly.

And the price? Well, my first monthly bill was $1.42, broken down thusly:

  • $0.15 per GB-Month of storage used – 2.478 GB-Mo – $0.37
  • $0.10 per GB-all data transfer in – 4.632 GB – $0.46
  • $0.18 per GB-first 10 TB/month data transfer out – 0.097 GB – $0.02
  • $0.01 per 1,000 PUT or LIST requests – 55653 Requests – $0.56
  • $0.01 per 10,000 GET and all other requests – 46 Requests – $0.01

As you can see, it makes for a very cost-effective backup solution.

Some apps I found useful for working with S3 (Note: OS platforms listed after each app):
Jungle Disk – Windows, OS X, Linux
S3 Browser – OS X
Cockpit – Windows, OS X, Linux (requires Java)
S3Sync – Windows, OS X, Linux (requires Ruby)