childthemes

This article is a continuation of our WP Basics series, specifically WP Basics – The World Of Child Themes. The post goes a little deeper into the workflow of using a child theme and will explain how you can use the power of a child theme to override things such as styling and functions, whilst keeping your main theme untouched so you can update your theme without losing your precious changes.

Overriding Styles with a Child Theme

One of the most common reasons to implement a child theme is to add your own CSS styling to a theme to really make it your own. If you have followed our previous article and used the excellent Child Theme Creator by Orbisius, you should already have a blank style.css file where you can apply your own styling, however if you are creating your own child theme from scratch, you can simply drop a style.css file into your child theme and begin applying your styling in no time.

When creating your own custom CSS, if your not familiar with your browsers developer tools (which are an essential part of custom styling) – you should check out our fabulous guide on Developer Tools here.

Overriding Functions with a Child Theme

For those looking to go a little deeper, its also possible to override entire PHP functions via a child theme, but be warned, not all themes are created in a way where this is possible. Here is an example function taken from our Foundry WordPress theme (what it does is not important here) of a function which can be overridden

if(!( function_exists('ebor_register_nav_menus') )){
    function ebor_register_nav_menus() {
        register_nav_menus( 
            array(
                 'primary' => __( 'Standard Navigation', 'foundry' ),
                 'offscreen' => __( 'Offscreen Navigation', 'foundry' )
            ) 
        );
    }
    add_action( 'init', 'ebor_register_nav_menus' );
}

So what makes this child-theme friendly? This line here

if(!( function_exists('ebor_register_nav_menus') )){

Basically, what this line does is check to see if the PHP function ‘ebor_register_nav_menus’ has already been declared, and if it hasn’t, it will create the function. This is crucial as if you where to simply copy a function of a theme which doesnt use this check, your going to run into a PHP error for sure.

So, if you wanted to modify this function for whatever reason, you would simply need to copy the function to your child themes functions.php file and your all set.

Overriding Template Files with a Child Theme

Finally, another great feature of using a child theme is the ability to swap out template files such as single.php (which handles single posts etc). The process here is also very simple, just copy the template file you wish to alter to your child theme, and edit as you wish. The child themes file will automatically be used so no further effort is needed, perfect!