It is always best practice when using a theme that either you bought or could be updated by someone other than yourself to create a child theme of the theme you are working within WordPress.

First thing you are going to want to do is to create a child theme folder. Create a new folder in the directory wp-content/themes. For example,if you were to make a child theme for twentynineteen then your newly created folder in the themes folder would be the following: twentynineteen-child.

The second thing to do is create a stylesheet inside the newly created child theme folder called style.css

/*
 Theme Name:   Twenty Fifteen Child
 Theme URI:    http://example.com/twenty-nineteen-child/
 Description:  Twenty Nineteen Child Theme
 Author:       John Doe
 Author URI:   http://example.com
 Template:     twentynineteen
 Version:      1.0.0
 License:      GNU General Public License v2 or later
 License URI:  http://www.gnu.org/licenses/gpl-2.0.html
 Tags:         light, dark, two-columns, right-sidebar, responsive-layout, accessibility-ready
 Text Domain:  twentynineteenchild
*/

The required information that needs to be supplied in your style.css are the following:

  • Theme Name – needs to be unique to your theme you are creating the child theme for
  • Template – the name of the parent theme directory. The parent theme in our example is twentynineteen. You may be working with a different theme, so adjust the code above to the correct theme you are creating for.

Next, create a functions.php in your child theme directory so you can call out the parent and child theme stylesheets or add any additional custom functions that are needed for your child theme.

The recommended way of doing this is to add a wp_enqueue_scripts action and use wp_enqueue_style() in your child theme’s functions.php. Note the first line of your functions.php should always open with the PHP tag.

<?php
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_styles() {
 
    $parent_style = 'parent-style'; // This is 'twentynineteen-style' for the Twenty Nineteen 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')
    );
}

The last step is to Activate your new child theme by going to your WordPress Dashboard screen and go to Appearance > Themes. You should see your new child theme listed and ready for activation.

Extra things that you can do for further modifications of the current theme is to copy over specific files that need to be adjusted or modified to meet your requirements.

Example if you wanted to modify the page.php page all you need to do is copy from the parent theme directory page.php and paste this into your child theme and start making your changes as needed. Your page.php in the child theme directory will over-ride the parents page.php file to be rendered.

Reference: WordPress Development Child Theme


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *