Created
October 12, 2015 11:07
-
-
Save onnimonni/8f58e72bc433c54f7b17 to your computer and use it in GitHub Desktop.
Child theme functions.php snippet which enqueues parent theme styles, loads languages from child theme and copies settings from parent theme to child theme.
This file contains 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 | |
/* | |
* Author: Onni Hakala / Seravo Oy | |
* Source: https://gist.github.com/onnimonni/8f58e72bc433c54f7b17 | |
* Description: Use these default options which are good for every typical child theme | |
* You can just copy-paste this into child-theme functions.php | |
*/ | |
if (!class_exists('ChildThemeOptions')) { | |
class ChildThemeOptions { | |
/* | |
* Parent theme slug | |
*/ | |
private static $parent_slug; | |
/* | |
* Theme slug | |
*/ | |
private static $theme_slug; | |
// Add our filters for the child theme | |
function __construct(){ | |
/* | |
* Enqueue child theme styles | |
*/ | |
add_action( 'wp_enqueue_scripts', array(__CLASS__, 'parent_theme_enqueue_styles' )); | |
/* | |
* Use translations from child theme | |
*/ | |
add_action( 'after_setup_theme', array(__CLASS__, 'child_theme_locale' )); | |
/* | |
* Copy parent theme options during child theme activation | |
*/ | |
add_action( 'after_switch_theme', array(__CLASS__, 'setup_options_from_parent_theme')); | |
// Set slug variables for later usage | |
self::$parent_slug = wp_get_theme()->get('Template'); | |
self::$theme_slug = basename(get_stylesheet_directory()); | |
} | |
/** | |
* Enqueue parent theme styles as well and make styles of this theme dependent on parent | |
*/ | |
public static function parent_theme_enqueue_styles() { | |
wp_enqueue_style( 'parent-theme-style', get_template_directory_uri() . '/style.css' ); | |
wp_enqueue_style( 'child-theme-style', | |
get_stylesheet_directory_uri() . '/style.css', | |
array('parent-theme-style') | |
); | |
} | |
/** | |
* Load translations for the parent theme so they are not removed when theme is updated | |
*/ | |
public static function child_theme_locale() { | |
load_child_theme_textdomain( self::$parent_slug, get_stylesheet_directory() . '/languages' ); | |
} | |
/** | |
* Many times users have already equipped the parent theme with correct options | |
* When activiting this theme just copy them from parent theme so that | |
* user doesn't have to setup them again. | |
* This is only done once. | |
*/ | |
public static function setup_options_from_parent_theme() { | |
$parent_options = get_option( 'theme_mods_'.self::$parent_slug ); | |
$options_already_setted = get_option( 'theme_'.self::$theme_slug.'_copied_from_parent', false); | |
if ($parent_options and !$options_already_setted) { | |
update_option('theme_mods_'.self::$theme_slug,$parent_options); | |
// We want this to only happen once so write it in to memory | |
update_option('theme_'.self::$theme_slug.'_copied_from_parent',true); | |
} | |
} | |
} // End Class: ChildThemeOptions | |
new ChildThemeOptions(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment