-
-
Save pareshsojitra/3103e31666650c58a470ce568704ef6a to your computer and use it in GitHub Desktop.
Use WP-CLI to quickly generate WordPress child themes. Visit blog post https://bit.ly/2VEkdyf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$options = [ | |
'parent' => '', | |
'name' => 'Child Theme', | |
'description' => 'Enter child theme description...', | |
'screenshot' => '', | |
'bootstrap' => false | |
]; | |
$bootstrap_header = $bootstrap_footer = ''; | |
$contents = scandir(ABSPATH . 'wp-content/themes'); | |
$found = false; | |
foreach ($args as $idx => $arg) { | |
if ($arg[0] == '-') { | |
$options[substr($arg, 1, strlen($arg))] = $args[$idx + 1]; | |
} | |
} | |
foreach ($contents as $idx => $entry) { | |
if (is_dir($entry) && !in_array($entry, ['.','..'])) { | |
if ($options['parent'] == $entry) { | |
$found = true; | |
break; | |
} | |
} | |
} | |
$child_theme_path = ABSPATH . 'wp-content/themes/' . $options['parent'] . '-child'; | |
if (!$found || is_dir($child_theme_path)) { | |
echo 'Error! Parent theme does not exist or child theme folder already found inside themes/.'; | |
exit; | |
} | |
if ($options['bootstrap']) { | |
$bootstrap_header = '<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">'; | |
$bootstrap_footer = '<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> | |
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>'; | |
} | |
mkdir($child_theme_path, 0755); | |
mkdir($child_theme_path . '/assets', 0755); | |
mkdir($child_theme_path . '/assets/css', 0755); | |
mkdir($child_theme_path . '/assets/js', 0755); | |
mkdir($child_theme_path . '/assets/img', 0755); | |
file_put_contents($child_theme_path . '/functions.php', "<?php | |
add_action('wp_enqueue_scripts', '{$options['parent']}_enqueue_styles'); | |
function {$options['parent']}_enqueue_styles() { | |
wp_enqueue_style('{$options['parent']}-styles', get_template_directory_uri() . '/style.css'); | |
wp_enqueue_style('{$options['parent']}-child-styles', | |
get_stylesheet_directory_uri() . '/style.css', | |
array( 'parent-styles') | |
); | |
} | |
?>"); | |
file_put_contents($child_theme_path . '/index.php', "<?php | |
get_header(); | |
?> | |
<h1>Hello, world!</h1> | |
<?php | |
get_footer(); | |
?>"); | |
file_put_contents($child_theme_path . '/header.php', '<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | |
' . $bootstrap_header . ' | |
<?php wp_head();?> | |
</head> | |
<body>'); | |
file_put_contents($child_theme_path . '/footer.php', "<?php wp_footer();?> | |
{$bootstrap_footer} | |
</body> | |
</html>"); | |
file_put_contents($child_theme_path . '/style.css', "/* | |
Theme Name: {$options['name']} | |
Theme URI: https:// | |
Description: {$options['description']} | |
Author: Author Name | |
Author URI: https:// | |
Template: {$options['parent']} | |
Version: 1.0.0 | |
*/"); | |
if (!$options['screenshot']) { | |
file_put_contents($child_theme_path . '/screenshot.png', file_get_contents(ABSPATH . 'wp-content/themes/' . $options['parent'] . '/screenshot.png')); | |
} else { | |
file_put_contents($child_theme_path . '/screenshot.png', file_get_contents($options['screenshot'])); | |
} | |
echo 'Success! Child theme `' . $options['parent'] . '-child` created successfully.'; | |
exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment