Create child-theme from any theme on wordpress

How to create a Child-Theme from any theme


Child Themes are the recommended way by WordPress of making changes to existing templates. And here is good reasons why:

  • If you modify a theme directly and it is updated, then your modifications may be lost. By using a child theme you will ensure that your modifications are preserved.
  • It’s a great way of creating a sub-theme for specific pages of your website, eg: Blog

It is very simple to create a child-theme from any theme.  Just follow these steps.

  1. Go to wp-content/themes
  2. Create a new folder called “mytheme-child”, where mytheme is the name of the parent theme
  3. Enter this folder. It should be completely blank
  4. Now create a file called style.css and another called functions.php
    Create child-theme from any theme on wordpress

    You should have something like this by now

  5. Open the parent style.css and copy it’s header.
    The header looks like this:
/*
	Theme Name: My Theme
	Theme URI: https://www.lucasbustamante.com.br
	Description: This is a description example
	Version: 1.0.0
	Author: Lucas Bustamante
	Author URI: http://wwww.lucasbustamante.com.br
	Tags: Lucas Bustamante, Programador
	License: MIT
	License URI: http://opensource.org/licenses/mit-license.php
*/
  1. Now paste that into your child’s style.css, and add only one line
    Template: mytheme
/*
	Theme Name: My Theme
	Theme URI: https://www.lucasbustamante.com.br
	Description: This is a description example
	Template: mytheme
	Version: 1.0.0
	Author: Lucas Bustamante
	Author URI: http://wwww.lucasbustamante.com.br
	Tags: Lucas Bustamante, Programador
	License: MIT
	License URI: http://opensource.org/licenses/mit-license.php
*/
  1. We are almost done! Just add this to functions.php
	// Register style.css for Child-Theme
	function register_child_style() {
		$parent_style="mytheme-style";
		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','register_child_style', PHP_INT_MAX);

There you have it!
Now just enable your new child theme under Appeareance -> Themes

To make changes to a child-theme, copy the original file from it’s parent and edit it. That’s where it’s true potential is!


Tag: