How To Protect Your WordPress Theme Against Upgrades

Problem: You’ve finally found a theme you like but you want to modify it. The modifications are pretty simple but what happens when you want to upgrade the theme? Do you really want to go through all those files again hunting down the changes? Don’t you wish you could just upgrade and be done with it?

I’ve been there. I’ve done everything the wrong way at least twice. Learn from my mistakes. Here’s the right way to modify your theme and protect it against any future upgrades. And don’t worry, it’s really simple. As it usually turns out, WordPress is ready for us and has done most of the heavy lifting.

Create a Child Theme for Simple Modifications

Did you know you can make CSS-only themes for WordPress? CSS-only themes pumped up with the power of custom PHP in functions.php? I don’t know why more people don’t know about this feature of WordPress or use it more often. It’s really powerful and clean and—more importantly—solves probably 99% of all your theme modification needs.

In wp-content/themes make a new folder called “sample”. Or “monstermonkey” or something. Try and make it unique. In that folder we’re going to make a file called style.css. Just like a regular WordPress theme. I’ll give you all the code for your style.css file straight up (adapted from the WordPress codex) and we’ll break it down afterwords. Here’s the code:

/*
Theme Name: Sample Theme
Theme URI: http://yourdomain.com/
Description: Testing WordPress Child Themes
Author: Your Name
Author URI: http://yourdomain.com/
Template: thematic
Version: 1.0
.
This work, like WordPress, is released under GNU General Public License, version 2 (GPL).
http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
.
*/
 
@import url('../thematic/style.css');
 
a:link {
    color:red; /* Red? Ugh! */
}
a:visited {
    color:green; /* Green? Yechh! */
}

The most important thing in this stylesheet is Template: thematic. This tells WordPress to use all the template files from another theme. In this case Thematic. But it could just as easily be cutline, default, sandbox, or anything—as long as it matches the folder of the Parent Theme, all in lowercase. The WordPress codex calls this defining a Parent Theme. Thusly: Child Themes. You just made one.

Next we’re going to import the stylesheet of our Parent Theme. Thanks to relative links and WordPress naming conventions this is insanely easy.

@import url('../thematic/style.css');

The ../ is the most important thing here. It means move up one directory level. This gives us the power to import the stylesheet of any theme in our wp-content/themes directory. While this is fun for making weird theme mashups (try defining classic as your parent theme and importing the default stylesheet) the real power of this technique comes in protecting your favorite WordPress theme from upgrades.

You can now edit the CSS of your favorite theme without touching the original file. This is the real power of Child Themes. And it’s huge.

WordPress Child Themes are THE best way to make custom CSS changes to your themes.

In the example above I’ve made a pretty broad declaration about links changing the links to red (ugh!) and the visited links to green (yechh!)—as long as they don’t have very specific CSS declarations made in the original stylesheet. But what if you want to change the menu styles of your theme or the header styles, where more specific CSS declarations are made? This is easy too. Simply copy the offending section of the original theme stylesheet into your new Child Theme stylesheet and—boom!—you can make the changes there, free from worry.

Important note: all this hinges on the Child Theme being the active theme. Otherwise, well, it won’t work. :)

Bonus for Thematic Users

If you want to start your Thematic Child Theme the exact same way I do, you’ll want to use the following code to start off your styles. It’s right out of the Thematic style.css, relativised for your Child Themes.

/* Reset browser defaults */
@import url('../thematic/library/styles/reset.css');
 
/* Apply basic typography styles */
@import url('../thematic/library/styles/typography.css');
 
/* Style the meta panel for logged-in users */
@import url('../thematic/library/styles/sitemeta.css');
 
/* Apply a basic layout */
@import url('../thematic/library/layouts/2c-r-fixed.css');
 
/* Prepare theme for plugins */
@import url('../thematic/library/styles/plugins.css');
 
/* Un-comment the line below to set a grid with 18px line-height to fit 125px ad units  */
/* body { background:url(../thematic/images/960_grid_12_col.gif) repeat-y top center; } */

Bonus For Themes Built Up From The Sandbox

Scott and Andy’s beautiful WordPress theme impresses me once again. With the upcoming version 1.6 of The Sandbox (and consequently Thematic version 0.4+) you can modify parts of your Parent Theme by overriding it in your functions.php of the Child Theme.

Here’s one way to add a home link to your Sandbox 1.6+ or Thematic 0.4+ theme without touching the code of your parent theme at all. Make a file called functions.php in your child theme and copy the following code into it, between opening and closing PHP tags (“<?php” & “?>” ).

function sample_menu() {
    $menu = '<div id="menu"><ul>';
    if ( is_home() ) {
        $menu .= '<li class="current_page_item"><a href="';
    }
    else {
         $menu .= '<li><a href="';
    }
    $menu .= get_option('home') . '/" title="Home">Home</li>';
    $menu .= str_replace( array( "\r", "\n", "\t" ), '', wp_list_pages('title_li=&sort_column=menu_order&echo=0') );
    $menu .= "</ul></div>\n";
    echo $menu;
}
add_filter('sandbox_menu', 'sample_menu' );

Now you’ve got a Home link in your menu! What else can you do? You can even add a changeable header using the WordPress Custom Image Header API. Check out this wiki article for more info on Child Themes (Scott calls them templates in his wiki).

Choose Plugins That Don’t Require Theme Modifications—Or Use The Right Kind of Theme

Confession time: I’m obviously completely comfortable with modifying WordPress themes but adding code for plugins and messing up my template files just rubs me the wrong way. It’s one more thing I have to worry about when I’m upgrading my site theme. If a plugin requires you to edit your WordPress theme reconsider that plugin. Is it really worth harming the upgrade-ability of your theme just to have that plugin?

Well, yeah. Maybe it is.

OK. Sometimes it is. But I have a solution. Use a different theme. Use a theme that comes ready for absolute must have plugins. Or a fully widgetized theme like Options, Vanilla or Thematic.

A plugin-ready parent theme takes the upgrade pressure off of you. Want Subscribe to Comments in your Theme? Thematic has it (until it moves to core—please!—move it to core!). Want to use Similar Posts in your theme after a single post? Thematic has a widget-ized area right after the single post. Just move the similar posts widget there.

It’s Really Not That Hard to Keep Your WordPress Theme Safe

Reconsidering the value of plugins, using a plugin-ready theme and using a fully widget-ized theme will make your theme upgrades a whole lot simpler. Add all that up with the power of Child Themes and you’re set.

Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

17 Comments

  1. Posted May 21, 2008 at 11:38 am | Permalink

    Excellent advice, Ian. Especially suggesting that better WP themes upgrade more easily. Plugins should use wp_head() wp_footer() and official WP hooks/actions when modifying your blog.

    Well, I better get to work on finishing up 1.6. ;-)

  2. Posted May 21, 2008 at 11:51 am | Permalink

    Good article Ian. I’m in the middle of a major redevelopment of how my Options theme works. I definitely didn’t make it “upgrade friendly.” So, that’s what the new development is all about.

    I don’t like adding plugin code in theme files either, but it’s a necessary evil sometimes.

    I’m glad you figured out the parent/child theme thing. I might have to use it one of these days. Or, just start designing child themes for you. ;)

  3. Posted May 21, 2008 at 12:14 pm | Permalink

    This is a great article.

    And this feature in WordPress could be very useful for theme authors as well, as there are many who really just ad a new style to the default, classic or another theme they find that they like.

  4. Posted May 21, 2008 at 12:59 pm | Permalink

    @Scott Funnily enough, I hadn’t noticed 1.6 wasn’t out until I started writing this post. I love svn.

    @Justin I haven’t exactly figured out how to make WordPress recognize new custom page templates yet, particularly for the front page. So no, I haven’t figured that out yet. But I have come up with a way to produce custom front pages with child themes—as long as there is a front page template in the Parent Theme. I haven’t decided if that’ll make it into Thematic though.

    @Kristen Exactly! :)

  5. Posted May 22, 2008 at 8:40 pm | Permalink

    Adding a note that the newly created child theme is the one that should active might help new users :-)

  6. Posted May 22, 2008 at 9:15 pm | Permalink

    Good point. Added.

  7. Posted July 7, 2008 at 1:49 am | Permalink

    Thanks Ian. This sounds like one of the best ideas of theme customization.

  8. Posted July 22, 2008 at 12:28 pm | Permalink

    I’ve seen the “Template:” thingy mentioned before, with very little explanation. So, thanks for enlightening me, Mr Stewart.

  9. Posted July 22, 2008 at 12:34 pm | Permalink

    No problem, Josh. Thanks for reading it. I like your current theme, by the way. Nice.

  10. Eric
    Posted July 28, 2008 at 4:42 pm | Permalink

    Dear Ian,

    I absolutely LOVE your Thematic theme. I just downloaded it and plan on using it for my site. Based on your article, I’d day Thematic is a great theme for protecting my wordpress theme against upgrades no?

    I plan on making some modifications to the look. Will let you know how it pans out.

    Thanks again! Eric

  11. Posted July 28, 2008 at 6:08 pm | Permalink

    Thanks, Eric. Cheers.

  12. Mike Tosetto
    Posted July 29, 2008 at 8:09 pm | Permalink

    Hi Ian

    I love the idea of adding a Home link to the menu but I’ve followed your instructions and am having no joy.

    Unfortunately, I can’t provide a link as I’m modifying your Thematic them locally before going live.

    Btw, it’s a great theme… many thanks. Mike

  13. Posted July 29, 2008 at 8:43 pm | Permalink

    That’s probably because I totally forgot to add the line …

    add_filter('sandbox_menu', 'sample_menu' );

    Sorry. Check it over again.

  14. Posted August 1, 2008 at 5:06 pm | Permalink

    Well… I’m a newbie jumping in with some old hands here, so apologies if my questions seem somewhat naive. I’m new to WordPress, but after lots of research, I’m keen on writing a child to Thematic to launch our blog. So first off: you and folks like you have done a great service to the community by working in this manner - kudos! My questions:
    1. First, you reference your parent theme’s style sheet. Then you add your new definitions. Those definitions are either added to your parent theme’s style sheet, or, if in conflict with them, given priority. Is this correct?
    2. If this is correct, why is it also necessary (or at least, recommended) to add those additional import lines referencing Thematic’s /library directory. Are these not automatically inherited, too?
    3. On a slightly different topic, would one attempt to make changes in these types of files in the same manner that one would work on a normal website (for example, by loading them into Dreamweaver or a similar app)?

  15. Posted October 24, 2008 at 11:48 am | Permalink

    Minor point - looks like you’re missing the closing tag for the anchor, so the line should read:
    $menu .= get_option(‘home’) . ‘/” title=”Home”>Home’;

    With that, and the change you mentioned in the forum (sandbox_menu has changed to globalnav_menu as of thematic 0.7), it works brilliantly!

  16. Posted November 4, 2008 at 8:07 am | Permalink

    Can I just say, WOW.

    I’m new to wordpress, HTML and FTP programs. I’ve come over from blogger where everything is done for you. I’ve been wondering around pretty aimlessly trying to do what I want to do but not knowing how. I put in a font question over in support and I don’t know if it was you who referred me here or someone else, but they’re a life saver!

    I am just super impressed with this web site. Thanks for this wonderful website.

  17. Posted December 15, 2008 at 10:07 am | Permalink

    Ian — Just wanted to let you know that I’m in the middle of creating a child theme locally for RawTake (dot net) using Thematic. So far, so good. I’ll be diving deeper into the forums and articles to help guide me more and as soon as we launch, I’ll let you know :-) Would love to get your thoughts. Thanks again for this article and many others.

19 Trackbacks

  1. […] How to Protect Your WordPress Theme Against Upgrades […]

  2. By WangenWeb Weekly Digest (21/08) | WangenWeb on May 26, 2008 at 1:27 am

    […] Tutorial: ThemeShaper: How To Protect Your WordPress Theme Against Upgrades The article tells you how to create Child Themes for a theme. Easily explained a Child Themes is a […]

  3. […] How To Protect Your WordPress Theme Against Upgrades (tags: wordpress css theme) […]

  4. […] How To Protect Your WordPress Theme Against Upgrades by Ian Stewart: Problem: You’ve finally found a theme you like but you want to modify it. The modifications are pretty simple but what happens when you want to upgrade the theme? Do you really want to go through all those files again hunting down the changes? Don’t you wish you could just upgrade and be done with it? […]

  5. By Protecting text strings from theme upgrades on June 17, 2008 at 3:00 pm

    […] 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 […]

  6. […] yet another why-didn’t-anyone-tell-me-sooner moment – child themes in WordPress.  From How To Protect Your WordPress Theme Against Upgrades (via Parent Child Themes and How I used a WordPress Child Theme To Redesign My Blog The Smart Way): […]

  7. By john keegan dot org » links for 2008-07-05 on July 5, 2008 at 12:35 am

    […] How To Protect Your WordPress Theme Against Upgrades Using Parent and Child CSS themes in Wordpress… Essentially this involves loading a theme and then overriding it with your own customizations. (tags: wordpress css theme howto themes) […]

  8. By Using Child Themes on July 13, 2008 at 8:40 pm

    […] How To Protect Your WordPress Theme Against Upgrades […]

  9. By Creating WordPress Child Themes | WangenWeb on July 19, 2008 at 2:56 am

    […] Themeshaper: How To Protect Your WordPress Theme Against Upgrades […]

  10. By Adapter un thème Wordpress - 6502.fr on August 16, 2008 at 5:01 am

    […] Via [ThemeShaper] […]

  11. […] themeshaper.com/how-to-protect-your-wordpress-theme-against-upgrades […]

  12. […] more information, check out this article on ThemeShaper. Wordpress, Wordpress Theme, […]

  13. […] themeshaper.com/how-to-protect-your-wordpress-theme-against-upgrades […]

  14. By links for 2008-09-28 | Arc Iris on September 28, 2008 at 3:04 pm

    […] How To Protect Your WordPress Theme Against Upgrades (tags: WordPress themes child) […]

  15. […] http://themeshaper.com/how-to-protect-your-wordpress-theme-against-upgrades/ […]

  16. By Changes… again! | Moonlit Minds on October 6, 2008 at 5:53 pm

    […] How To Protect Your WordPress Theme Against Upgrades another introduction - a bit more information. […]

  17. […] and makes good use of the Child themes (if you are not clear with these ones, go have a look at Themeshaper’s post about it). The result can be compared to the Woo Themes, although there is a free membership […]

  18. By links for 2008-11-12 « Mike’s Blog on November 12, 2008 at 5:09 pm

    […] How To Protect Your WordPress Theme Against Upgrades (tags: wordpress) […]

  19. By links for 2008-11-13 | /dev/random on November 13, 2008 at 1:05 pm

    […] How To Protect Your WordPress Theme Against Upgrades (tags: wordpress) […]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/web site in attribution. In addition, you may find yourself fitter, happier and more productive. Comment away.

Subscribe without commenting