Create a child theme - always the first step

There is no magic button in the core WordPress to create "child themes', so the below code will handle the process. If you already have a plugin to do this task, you do not need this.

Edit the current theme's functions.php with the Appearance > Theme Editor and add the code at the bottom. Yes the theme editor will throw a notice that you are editing a theme's core and it will be overwritten on update, and that's OK. You'll already have the child theme long before then, and the core file is fine to be overwritten.

The code will add a menu button labeled Make Child to the side navigation in the admin. Once you click the menu item, it will redirect to a page with URL admin.php?page=makechild

make-child-theme-menu.jpg

 

Successful Copy

When the child theme duplication is done, a confirmation message will display, and the page will redirect to the themes manager at themes.php to allow for new child theme activation.

child-theme-complete.jpg

 

The Code

  • Copy the entire coding, including the commented lines
  • Go to Appearance > Theme Editor
  • Click Theme Functions (functions.php) from the file list on the right.
  • Scroll to the bottom of the file
  • Add 2 line spaces and paste, then click Update File. When the page reloads, the button will be visible.
## Method To Create Child Theme

if( !file_exists(get_template_directory().'-child') ) 
{
	// add admin side bar button to make child theme
	add_action('admin_menu', function()
	{
		add_menu_page('','Make Child','manage_options','makechild',function()
		{
			echo '<h2>Creating Child Theme Of '.wp_get_theme()->get('Name').'</h2>';
			echo '<p>The process may take about 10 seconds</p>';
			// processor
			makeChild();
			
		},'dashicons-welcome-widgets-menus',99);
	});

	// make theme duplicate
	function makeChild()
	{
		$src = get_template_directory();
		$dest = $src.'-child';
		makeCopy($src, $dest);
		
		sleep(5);
		
		$name = wp_get_theme()->get('Name');
		$childname = $name.' Child';
		$uri = wp_get_theme()->get('ThemeURI');
		$childuri = substr($uri,0,-1).'-child/';
		
		// get file content as array
		$st = file(get_template_directory().'/style.css');
		
		//search file for theme name and theme uri to replace with child theme details
		foreach($st as $k => $v) {
			if( strstr($v, 'Theme Name') )
			$tn[$k] = 'Theme Name: '.$childname."\r\n";
			
			if( strstr($v, 'Theme URI') )
			$tu[$k] = 'Theme URI: '.$childuri."\r\n";
		}
		
		// replace and rewrite file
		$style = array_replace($st, $tn, $tu);
		file_put_contents($dest.'/style.css', implode('',$style));
		
		// confirm successful child duplication
		$child = basename(get_template_directory()).'-child';
		if( file_exists(get_template_directory($child).'/style.css') ) {
			echo '<p>Child '.$childname.' theme creation complete!</p>';
			echo '<p>Redirecting to themes manager for activation step...</p>';
			echo '<meta http-equiv="refresh" content="3; URL='.admin_url('themes.php').'">';
		}
	}
	//recursive file copy
	function makeCopy($src, $dest) 
	{
		if( is_dir($src) ) 
		{
			mkdir($dest);
			$files = scandir($src);
			
			foreach($files as $file) {
				if( $file != "." && $file != ".." )
					makeCopy("$src/$file", "$dest/$file");
			}
		}
		else
		if( file_exists($src) ) {
			copy($src, $dest);
		}
	}
}

Now it's on to making all the great functionality tweaks in your theme, without need to install hundreds of minuscule plugins just to handle simple tasks. Get rid of the clutter and be safer.




Copyright © 2021 CMSEnergizer.com. All Rights Reserved.
File download and article manager component by CMSE Custom Fields | Website hosting and development by WebsiteDons

CMSEnergizer.com is owned and operated by Emuzement Net Inc, Tampa, Florida USA