Pages

WordPress Child Themes Development

There are a number of reasons that WordPress developers are starting to use child themes. They give you the opportunity to customize a unique layout on top of another existing theme. This is perfect for beginners who want to play around with building their own themes.
wp child themes
Additionally many premium designs will release new updates over time. If you are making changes to core theme files they will be overwritten when performing an update, but child themes are separate and neatly tucked away. This means you can build off existing premium themes and save loads of time in the process.
In this guide I want to introduce the basic concepts of building a WordPress child theme and why it’s such a good idea.

Getting Started

Child themes are not as difficult as they may appear. The benefits of working off a parent theme means you don’t need to write all the HTML/CSS from scratch. A child theme will automatically use any template files you include, such as sidebar.php or footer.php. But if they are missing, then your child theme will pull the same files from its parent.
This functionality offers an enormous amount of freedom to customize already existing templates. It’s also great for touching up areas around your website for special events, such as adding design patterns for Christmas or New Years.

Your Required Files

You only need a single .css stylesheet to set up a child theme in WordPress. You also need to create a new directory in the /wp-content/themes folder which will house your child theme. Pay attention that you aren’t creating this folder inside the parent theme, but right alongside with it in the same themes directory.
Developers will often include a functions.php and screenshot.png in the same folder as your new CSS file. The screenshot is displayed in your WordPress admin panel and the functions theme file can be used for tons of backend changes.
But for now we should focus on the main stylesheet. This is commonly named style.css and includes a comment header with key meta information. This is important because your theme will only display as a child if you include the parent’s directory name. Below is an example header comment:
  1. /* 
  2. Theme Name:   Twenty Eleven Child 
  3. Theme URI:   http: //example.com/ 
  4. Description:  Child theme for the Twenty Eleven design 
  5. Author:     Jake Rocheleau 
  6. Author URI:   http: //www.hongkiat.com/blog/ 
  7. Template:    twentyeleven 
  8. Version:    0.1 
  9. */  
The value for template should be the directory name for the accompanied parent theme. Other than that all the other tags should be familiar to standard WordPress theming.
Newest demo 2011 twenty eleven theme
Although all the parent PHP templates will be used, the original parent’s style.css will not be imported automatically. If you want to work off the original styles you’ll have to include it at the top of your child’s style.css document. Below is an example including the WP Twenty Eleven theme.
  1. @import url("../twentyeleven/style.css");  

Setting Up New Styles

Applying CSS rules to your theme is just as easy as editing the original. If you know which elements you need to target then use the same selectors in your own child theme.
We could demo with some really easy changes to links and paragraphs. I’ve used code from the original Twenty Eleven theme for targeting the different elements. At times it is necessary to use a more specific selector to override the older design.
  1. body {  
  2.     padding: 0 1.4em;  
  3. }  
  4. #page {  
  5.     margin: 1.667em auto;  
  6.     max-width900px;  
  7. }  
  8.   
  9. a {  
  10.     color#5281df;  
  11.     text-decorationnone;  
  12.     font-family: Calibri, TahomaArialsans-serif;  
  13. }  
  14. a:focus,  
  15. a:active,  
  16. a:hover {  
  17.     text-decorationunderline;  
  18. }  
In these changes I’ve reduced the overall body size and also removed some padding from the edges. All of these selectors can be found listed in the original .css document. It’s notable that I’m also changing some properties for all anchor links which include a different font stack and color choice.

The !important Things

CSS has a special declaration to mark priority above other styles. The syntax is displayed as !important beginning with the exclamation mark and terminating at the end of your CSS property. This is necessary if you have cascading styles from a parent theme which are overriding your own custom rules.
  1. a {  
  2.     color#5281df !important;  
  3.     text-decorationnone;  
  4.     font-family: Calibri, TahomaArialsans-serif;  
  5. }  
Above I have copied my original changes and edited the anchor text color with an important clause. This will take precedence over all other styles of the same selector depth. More defined elements (such as #access li:hover > a) will usually hold their own styles unless the color was still inherited from our original selector. In this case our parent theme does not setup a font-family property on anchor links, so as such we don’t run into any inheritance issues.
CSS !important properties syntax
If you are having trouble making your changes stick, try popping one of these important marks at the end of your property statement. This isn’t a perfect fix for every inheritance problem, but it does come in handy a lot more frequently than you’d think.

Cloning functions.php

Unlike the main stylesheet, your child theme will import its parent’s functions automatically. This means you don’t need to copy over any of the PHP code to still have it active in your new theme. Yet if you’d like to re-define some of the functions you can build another functions.php and write in your new code with any changes.
As an example I have built a function which parses a few JavaScript files when the template initiates. This will remove any older versions of jQuery and SWFObject scripts, while simultaneously adding the most recent versions to the wp_head area.
  1. // queue js files for load  
  2. function mytheme_js()   
  3. {  
  4.  if (is_admin()) return;  
  5.   
  6.  wp_deregister_script('jquery');  
  7.  wp_register_script('jquery''http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js');  
  8.  wp_enqueue_script('jquery');  
  9.   
  10.  wp_deregister_script('swfobject');  
  11.  wp_register_script('swfobject''http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js');  
  12.  wp_enqueue_script('swfobject');  
  13. }  
  14. add_action('init', mytheme_js);  
I should point out that if you are importing code from the parent functions.php then you’ll have to use a different function name. Otherwise PHP will give out a fatal error and you will have to FTP into the server to fix the mistake.

Working with Theme Files

The most broad category of theming is building custom layouts and page types. By default your child theme will inherit all of its parent’s theme files. But you have the option of creating new child theme files and WP will register these as the ‘primary’ template.
For example archive.php and index.php are used to display the post archives and homepage screen, respectively. If there are changes you’d like to make which require edits to the HTML then you’d be safer cloning the parent files and editing them in the child’s theme directory.

Custom Page Templates

While we’re talking about template files I also want to introduce a piece of WordPress functionality which many are not familiar with. You can build page and post templates which will be selectable from the Admin panel when creating new content. Even if the parent theme doesn’t have the new custom template file WordPress will still use the child in place of a page.php or single.php.
First create a new file named page-offer.php. This will be a “special offer” promotional page which is themed differently than all the others. In here you can copy over your original page code or even build the theme entirely from scratch. The only code we need to let WordPress know about this new template is a comment setup in PHP.
  1. <?php   
  2. /**  
  3.  * Template Name: Offer 
  4.  */   
  5.   
  6. ?>  
Another alternative to this method is building custom pages named after the unique ID number. So instead of loading the default archive.php for author pages you could create a file such as author-ID.php where ID is the user’s unique WordPress ID number. Although this system is more taxing since you’d need to create a new template file for each of the authors on your site.
It becomes more useful if you can combine these two techniques with other template files. Notably categories and tags work well using their own theme files. Also if you link to attachments in your content then you’ll want to consider the different possible template layouts for each mime type. I included the template hierarchy below for a simple JPEG image attachment:
  • image.php
  • jpeg.php
  • image_jpeg.php
  • attachment.php

Helpful WordPress Tools

WordPress itself has a versatile plugins system that can manage a whole lot of customizations. Since child themes are so new, there aren’t a whole onslaught of 3rd party releases (yet). However there are a couple of tools you may check out to make your development time a bit shorter.
An obvious mention is the One-Click Child Theme plugin built and tested for the latest WordPress 3.x version. It adds a menu link into your Admin “Themes” section to automatically build a child using your currently active theme. This is fantastic if you don’t want to mess with FTP and are looking to play around with some new ideas.
If you plan to edit these files within the admin panel you’ll also enjoy clearer syntax highlighting. This isn’t offered in WordPress by default but you can install the Advanced Code Editor for some much-improved functionality. This makes wading through PHP code blocks and HTML/CSS a whole lot more manageable.

Additional Resources

Along with all the tips in this guide I want to share a set of important links for theme developers. There are already so many great articles and free child themes you can check out to study deeper into this subject. I added a wonderful collection of these resources below:

Conclusion

I hope the process of building WordPress child themes is clearer for you after reading this article. I’ve tried to explain how child themes can inherit both CSS and PHP templates from a parent. Additionally it’s very simple to manipulate specific files and create your own unique themes.