Creating a Simple Child Theme Using Twenty Eleven

One of the first things I noticed when I started using WordPress was how well it employs the developer's mantra, "Separation of Concerns." In the programming world, you want each component to rely on the other components as little as possible; this makes each component more reusable. A perfect example of this in WordPress is Themes. They totally separate the design from the content. We can change the theme, and the content remains unchanged. WordPress does something similar with they way we can build on themes in WordPress. Instead of modifying an existing theme (and risk losing the changes at the next update), we can create a child theme, which separates our changes from the parent. In this tutorial, I will show you how to create a simple child theme.


What is a Child Theme?

From the WordPress Codex:

In essence, what this means is we can build upon the parent theme without modifying its files. We can change the CSS, add a custom post type, or build in our own set of functions. This is also the recommended way of creating a theme based on another theme.

Aside from child themes, there are two other ways we can create a theme that's based off of another. The first way is to just modify that theme. However, if we do that and the theme is updated, we will lose all of our changes. The second way is to copy that theme into another folder and go from theme. We wouldn't lost the theme (provided we renamed it properly), but we'd have duplicates of all of the files, even the ones we didn't modify.

"A WordPress child theme is a theme that inherits the functionality of another theme, called the parent theme, and allows you to modify, or add to, the functionality of that parent theme."

In this tutorial, we will be modifying the latest default theme (as of 3.2), Twenty Eleven


The Twenty Eleven WordPress Theme

Step 1. Set-Up/Required Files

The first thing we'll want to do after upgrading WordPress to 3.2 or downloading and installing Twenty Eleven is create out own directory for our child theme. In /wp-content/themes/ create the directory twentyeleven-child. We can now start adding files to our child theme.

Style.css

There is only one required file when creating a child theme: style.css. It is here that we will declare our theme, as well as tell WordPress that it is the child of another theme. Create style.css in the twentyeleven-child folder and add the following lines:

Most of the above information is what you'd fine in any other theme: it's name, link, Author credit, and version. However, there is also a line for Template. This is where you tell WordPress what it should use for the parent theme. It must be the name of the directory of the parent theme, and it is case-sensitive.

One thing to note before we begin adding our own custom CSS: Our child theme's style.css file will completely over-write the parent's. Therefore, after our theme declaration info, our first line should import the parent's CSS:

We're now ready to modify the parent's CSS. While we're going to do some simple changes here, if you'd like to do more advanced editing I'd suggest using Firefox with Firebug or Google Chrome's Developer Tools to help you out. They are quite useful! For now, add the following lines to your style.css file, under the @import line:

These lines will change the background and font-color of the body, header, and comment boxes. You, of-course, can play around as much as you'd you :-)


Here's what we have so far...

Step 2. Functions.php

If I were to rank WordPress theme files in order of importance, I would put style.css and index.php in a tie for first as you need them to run. At second I would put functions.php. The functions.php file is how you add custom functionality to your theme: custom types, menu and thumbnail support, extra sidebars, and modifications for the loop, amongst other things.

While functions.php is not required for a child theme (remember, style.css is the only required file), it is the best way hands down to add in your own functionality. And unlike style.css, which overwrites the parent's style.css, our child's functions.php is loaded before the parent's. We're going to create a widget area for the header and override one of Twenty Eleven's built-in functions.

First, we'll add the widget area. Create your functions.php and add the following code:

This will register the widget area that we'll add to the header in the next step.

Now we will override one of the built-in Twenty Eleven functions. The Twenty Eleven makers made it very easy for child theme developers to override their functions, as they first check to see if a function of the same name exists. Take a look at the example in twentyeleven/functions.php that we'll be modifying:

Notice how the first line checks to see if there is a function by the name twentyeleven_posted_on() first. By placing a function with that name in our child theme's functions.php, we will override the parent theme's function.

Note for theme developers:If you'd like to make your theme easier for child theme developers ('plugable'), you should follow Twenty Eleven's lead and make sure a function by the same name does not exist for each of your custom functions as well!

The function prints a line after the post title with the date and hidden author information. We'll be changing this line a little bit using the same function call. Add the following code to your functions.php file:

The first thing we did was put all of the values we are using in variables. This is a personal preference of mine as I feel the code is a bit easier to read. As you can see, we didn't change too much here. First, we printed the time as well as the date. Second, we unhid the byline, so people could see the link to the author's other posts.


Here's our new posted data.

Again, while we didn't do any groundbreaking, we were able to demonstrate how to override a [properly coded] custom theme function. Now let's move on to the actual template files!


Step 3. Template Files

The last thing I want to cover here is template files for the child theme. Maybe we want to modify the header for the site, or the single post loop, or even add an archive page. That and more is possible using child themes. We would just name our file the same name as whatever file we want to replace in the parent. These files, like style.css, will override the parent's file. We'll be modifying the header and creating a new page template.

First we'll do the header. Create header.php, copy the code from twentyeleven/header.php and paste it into our header.php file. Once we do that we're going to replace lines 104 and 109 (<?php get_search_form(); ?>) with the following function call:

Now we'll add the following code to our functions.php file:

This code will make it so if there are any widgets in the Header Widget area, we show them, otherwise we'll show the search bar. Finally, we'll need a bit of CSS to make our header widget-friendly. Add this to your style.css file:

This will style the widget the same way the search box is styled, but be careful. This method isn't exactly bulletproof. It's just to demonstrate modifying template files in child themes. Here is our result:


Our header widget

Next Up, we'll create a new page template. Create the file twentyeleven-child/page-welcome.php and copy the code from Twenty Eleven's page.php (twentyeleven/page.php). The first thing we'll need to do is modify the comments at the top to let WordPress know this is a page template. Replace the comments at the top with this:

When we create a new page, we'll be able to use the "Welcome" template. What we're going to do now is add a custom message to the top of the page. You're entire file should look like this:

Note that in between the #primary and #content divs, we've added a div with the class welcome. Let's go to our style.css file and add some CSS for it:

Our end result should look like this:


The Welcome page template

I should mention that the better way to do this would be to add a custom field to that template, but like I said earlier, I wanted to keep things simple so we could focus on creating the child theme.


Conclusion

While the post would get a little long for me to re-list the code in each of the files, I have included the child theme for you to download. Obviously, we just scratched the surface of creating child themes; my goal was to give you a good idea of how to create one based on how WordPress handles each type of file. One thing to note is that when referencing images or other files from your child theme, you need to use get_stylesheet_directory_uri() as opposed to get_template_directory_uri() as the latter will link to the parent theme.

Remember, you can create a child theme from any WordPress theme. However, there are also a bunch of theme frameworks out there from which we can create pretty in-depth child themes. Some of the more popular ones include Carrington, Thematic, and Thesis. They each have a learning curve, but they are all very powerful!

Tags:

Comments

Related Articles