WordPress And Child Themes

Child themes in WordPress can seem complicated and unnecessary for the inexperienced. The truth is that the child themes are not as complicated as many people think, and there are several advantages to them.

What is a child theme?

A child theme is a theme based on a common theme, usually referred to as parent or the main theme. It is the simplest form of WordPress theme and making changes through a child theme instead of directly into the main theme has several advantages:

  • When the main theme is updated, you avoid losing your changes
  • Faster to work with
  • Easier to work with for beginners

But, as with most things, there are also disadvantages:

  • It uses most of import, which is bad for the site's performance
  • With many changes, the style sheets are unnecessarily long.

If there are high demands for performance and plan to make very many changes, it is in other words, better to make a common theme than a child theme. For most, however, may very well run with a child theme.


It is not possible to create a child theme with another child theme as a parent.

How does the child themes work?

Consider an example theme called "My theme" have only the following files:

  • style.css
  • header.php
  • footer.php
  • functions.php

When working with child themes, there are two main things you should remember:

  • The PHP files that are not in the child theme instead retrieved from the main theme.
  • The exception is the functions.php where both are retrieved - first from the child theme, then from the main theme.

How it works:

WordPress looks first if the file exists in the child theme - if it is not there to retrieve it instead of the main theme. This means that when working with children themes, you just copy the PHP files that you intend to change anything in. In the future, an update to the main theme so you can safely download the update without the changes you made will disappear.

And functions.php?

As I told you earlier WordPress downloads the functions.php first from the child theme and then from the main theme. This allows you to replace the functions of the main theme with your own.

In a common theme can be a function of the file, for example, look like this:

if (! function_exists ( 'do_something' ))
{
function do_something ()
{
// Do something
}
}

Since the code checks the function do_something () does not exist, and that the code will run after our own functions.php , so we can easily replace the function of our own:

function do_something ()
{
// Do something else
}

How to create a child theme?

Creating a child theme is very simple. Open the folder / wp-content / themes / and create a new folder there. It is a good practice to rename the folder to a child theme to it the same as the main theme of "-child" end, and since "My Theme" will have "Twenty Thirteen" as the main theme so we baptize folder to twenty thirteen-child.

Stylesheet

In the folder, we create a new file called style.css with the following contents:

/ *
Theme Name: My theme
Template: twenty thirteen
* /

/ * Import stylesheet from the main theme * /
@ import url ( '../twentythirteen/style.css' ) ;

Note that Template: twenty thirteen is indicating that this is a child theme of "Twenty Thirteen", the folder named twenty thirteen .
More than that is not necessary. If you save the file, log into your admin panel and go to Appearance> Themes and you'll see your new child theme there and activate it. Since you have not made any changes, you will not, however, so any difference between child theme and the main theme.

The row with import tells the browser to fetch a different style sheet, in this case is whether the original style sheet from the main theme. Any changes you make to the style sheet must occur after the import , otherwise they will not apply.
To find it you want to change the style sheets I recommend development tool that is built into your browser.

PHP files

Say you want to change any HTML code in the header of your website. Then copy the twenty thirteen / header.php to twenty thirteen-child / header.php , then make your edits in the new copy. As I explained earlier, WordPress will now fetch header.php from your child theme and the rest of all PHP files from the main theme.

Afterword

It's actually not as simple as creating a child theme. If you want to immerse yourself more so are the rest well documented.
What do you prefer to work with - children themes or common themes?

Comments