Creating Child Theme in WordPress

Creating Child Theme in WordPress

Only a handful of WordPress users uses a child theme and that’s because many of the users don’t know what is a child theme or Creating Child Theme in WordPress. Well, most of the people using WordPress tend to edit or customize their theme but all that customization is lost when you update your theme and that’s where the use of child theme comes. When you use a child theme then all your customization will be saved and you can easily update the parent theme.

Creating Child Theme in WordPress

Creating Child Theme in WordPress

Creating a Child Theme from an Unmodified Parent Theme

In order to create a child theme in WordPress you need to login to your cPanel and navigate to the public_html then wp-content/themes where you have to create a new folder for your child theme (example /Twentysixteen-child/). Make sure you don’t have any spaces in the name of child theme directory which may result in errors.

Recommended: You can also use One-Click Child Theme plugin to create a child theme(only from an unmodified parent theme).

Now you need to create a style.css file for your child theme (inside the child theme directory you just created). Once you have created the file just copy and paste the following code (Change below details according to your theme specifications):

/*
 Theme Name: Twenty Sixteen Child
 Theme URI: http://example.com/twenty-sixteen-child/
 Description: Twenty Sixteen Child Theme
 Author: WordPress Team
 Author URI: http://example.com
 Template: twentysixteen
 Version: 1.3.0
 License: GNU General Public License v3 or later
 License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/

Note: The Template line (Template: twentysixteen) is to be changed according to your current name of the theme directory(the parent theme whose child we are creating). The parent theme in our example is the Twenty Sixteen theme, so the Template will be twentysixteen.

Earlier @import was used to load the stylesheet from parent to the child theme, but now it’s not a good method as it increases the amount of time to load the stylesheet. In place of using @import its best to use PHP functions in your child theme functions.php file to load the stylesheet.

In order to use functions.php file you need to create one in your child theme directory. Use the following code in your functions.php file:

<?php
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_styles() {
    wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );

}
?>

The above code only works if your parent theme uses only one .css file to hold all the CSS code.

If your child theme style.css contain actually CSS code (as it normally does), you will need to enqueue it as well:

<?php
function my_theme_enqueue_styles() {

    $parent_style = 'parent-style'; // This is 'twentyfifteen-style' for the Twenty Fifteen theme.

    wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'child-style',
        get_stylesheet_directory_uri() . '/style.css',
        array( $parent_style ),
        wp_get_theme()->get('Version')
    );
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
?>

It’s time to activate your child theme, login to your admin panel then go to Appearance > Themes and activate your child theme from the available list of themes.

Note: You may need to re-save your menu (Appearance > Menus) and theme options (including background and header images) after activating the child theme.

Now whenever you want to make changes to your style.css or functions.php you can easily do that in your child theme without affecting the parent theme folder.

Creating Child Theme in WordPress from your parent theme, but most of you have already customized your theme then the above method is not going to help you at all. In that case, check out how to update a WordPress Theme without losing customization.

If hope this article was helpful to you but if you still have any question regarding this guide please feel free to ask them in comments.

Was this article helpful?

Related Articles

Leave A Comment?