Justin Tadlock

Protecting text strings from theme upgrades

Have you ever wanted to change some text that reads “Continue reading” or “Post written by” in your WordPress theme to something a little different?

That’s pretty easy to do if you just open the appropriate PHP file and edit it. But what happens when the theme is updated with some cool, new feature or for the next version of WordPress? Well, you have to change that text all over again.

Wouldn’t it be much easier to simply change the text once and not be hassled by theme upgrades?

Something all WordPress theme authors and users could use a little more of is protection from this. It’ll take a little work from both ends though.

Ian Stewart has already detailed some ways to to protect themes against upgrades. I wanted to take his idea little step farther by showing another simple way to help in this regard.

We’ll basically be using the same techniques as theme and plugin translators use. If you’ve ever translated any of my themes, you deserve some major respect. The Options theme alone has 220 different text strings!

If you look under the hood of some of my themes, you might find some weird PHP that looks like this:

<?php _e('About the Author','options'); ?>

Your average theme might look something like this:

About the Author

The PHP from my theme allows for an easy way to change text strings without ever touching that line of code.

Tools you’ll need

First, you need a theme that is ready for localization. I haven’t seen many around, but both my Options and Structure themes are ready for translation.

You need to grab a copy of a neat little tool called Poedit. It’s what we’ll be working with.

Then, you need to read the best article there is around on localization from Weblog Tools Collection. I won’t bother going into the details of this process since the WLTC post is in depth enough.

Once you’ve done all of the above, you’re set.

Getting your new text strings to work

Let’s suppose you’ve played around with this Poedit thing a bit and finally decided to change some words around on your site. For this example, I’ll be using my Options theme.

Open “en_EN.po” from your Options theme folder in Poedit. You’ll be brought to a screen with a lot of text strings. In the bottom left, after clicking on a text string, you can alter it. After you change a few, your screen will look a little like this:

Using Poedit to protect your themes against upgrades

You don’t have to change every string. Just change the ones you want to be different.

Save this file as “wacky.po” for now. This and a file named “wacky.mo” should be saved in your Options theme folder. After this, go to the root of your WordPress folder. Open the file named “wp-config.php” in a text editor.

Find this bit of code:

define ('WPLANG', '');

Change it to this:

define ('WPLANG', 'wacky');

Save everything. Refresh your page. Your new text strings should be there.

Fun with the Options theme

I figured I’d give this a good go myself and tried to have a little fun with it. Here’s what I’ve come up with. Take a look at the two tabs sections and compare.

Options theme tabs widget

Notice the difference between the two sections? I never touched a line of code to get this effect. If you want to see my own personalization of text strings in the Options theme, just download this file:

Okay, the language isn’t too wacky. Just drop wacky.po and wacky.mo in your theme folder. Then, remember to change the settings in wp-config.php.

Encouraging others to follow

I know this isn’t the coolest thing since the birth of Alan Turing, but it will help, especially if you’re even a little bit geeky.

If you’re a theme author, you should probably be making your themes ready for translation anyway. If your theme doesn’t support localization, encourage your theme author to update the theme.

Anyway, have fun with it.

Here are some good resources on localization (especially for theme and plugin authors):