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, just copy the Thematic Sample Child Theme included with your Thematic download into your wp-content/themes directory. Simply change the copied folder name and the name of the theme in the copied style.css. Activate it and you’re ready to edit your WordPress theme the smart way.

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.

79 responses

  1. […] guide code snips chikds […]

  2. […] as a tem­plate but with­out actu­ally touch­ing the frame­work theme files. See this post on Theme­Shaper for fur­ther […]

  3. […] Der er blot problemer med at rette i disse filer. Det væsentligste er måske, at temaerne opdateres, nogle med jævne mellemrum, andre, når der kommer en ny version af WordPress, som gør det nødvendigt. Hvis man ikke er meget forsigtig, kommer man nemt til at overskrive ens ændringer. Faktisk kan det næsten kun undgås, hvis man bruger et program, der kan sammenligne to eller tre (sæt af) filer på samme tid. Eller hvis man bruger et Child Theme. […]

  4. Love the theme man.

    I really need to add a Home tab in the navigation bar.

    I’m no wizz here. Any help peeps?

  5. […] Link: How To Protect Your WordPress Theme Against Upgrades […]

  6. doohh! That is nice and simple enough directions 😉

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

  8. […] Tarski talks about the possibilities opened. reference.sitepoint.com/css SitePoint CSS Reference. themeshaper.com/how-to-protect-your-wordpress-theme-against-upgrades The author of the theme Thematic explains how to make child themes. — The alliteration is […]

  9. That is a superb and simple solution to a rather complex problem.
    Thanks loads for this

  10. […] This has also introduced me to a feature of WordPress that I was previously unaware of — child themes. The problem with installing a new theme, and then tweaking it to meet your needs, is that, when […]

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

  12. Could not be better than this. Perfect article for all who hate WP upgrades including me.

    Gotta try it.

  13. This theme has been discontinued. It is no longer available for download.

    1. I’m writing about “Options” theme.

  14. Thomas Ericsson Avatar
    Thomas Ericsson

    How do I do a child of a child? (A child of Thematic Power Blog).

    Thomas

    1. Right now there is no way (well, sort of, mostly). And for Power Blog in particular it’s meant as a Theme you can hack up to start your development with. I would just keep a fresh copy on hand if you’re worried about it.

      1. @Ian, thanks for this answer. I’ve been trying to find out about grandchild theme support, and I checked out your WordPress ticket on this issue and the associated diff’s and I see that there is no support for the parent discovery to work recursively up the theme hierarchy, unfortunately, it’s a very simplistic implementation at the moment. However, you say well, sort of, mostly’ can you share with us what are your suggestions then for doing this? With child themes becoming ever more popular, proven and suggested methods for extending child themes while keeping them future-proof for upgrades will be very important for child themes to truly reach the level of adoption we would all like to see them at.

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

  16. […] This tutorial really got me off on the right foot. It explains everything. Better, it gets you excited about what you can accomplish and makes you feel stupid (in a good way) for not finding out about this sooner. […]

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

  18. […] allen validen Eigenschaften von strict XHTML. reference.sitepoint.com/css SitePoint CSS Referenz. themeshaper.com/how-to-protect-your-wordpress-theme-against-upgrades Der Autor des Themes Thematic, Ian Stewart, erläutert wie Child Themes erstellt werden. […]

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

  20. Ian, how would using a child theme to protect your changes compare with moving the entire wp-contents folder (something I have not been able to do successfully)? My primary interest in moving the folder was to protect against changes during upgrades.

    I ask because I have made changes to a plugin file (in the plugins folder) for the purpose of correcting invalid code. I keep a changelog and have had to re-correct the code after the plugin upgraded.

    Do you foresee a time when even changes to upper level files can be preserved? For instance, I have edited wp-includesdefault-widgets.php for the same reason.

    I shall have to take a look at using Thematic, too bad I am neck-deep in my current theme.

    Cheers

  21. Hi,

    I just made the update of the thematic theme (now version 0.9.7.6) and it changed the way my website looks like, I used to have 4 colums of thumbnails and now there is only 3 and I have no idea how to fix it, does anyone can help please?

    Pierre-Luc

    1. This is a great question for the forums.