[wp-trac] [WordPress Trac] #27177: Child themes should inherit parent theme customizer settings on activation / switching
WordPress Trac
noreply at wordpress.org
Sat Mar 28 14:05:53 UTC 2020
#27177: Child themes should inherit parent theme customizer settings on activation
/ switching
-------------------------+------------------------------
Reporter: krogsgard | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Themes | Version: 3.8
Severity: normal | Resolution:
Keywords: has-patch | Focuses: administration
-------------------------+------------------------------
Comment (by yura1980):
Replying to [comment:14 greenshady]:
> As I really dislike the idea of the initial proposal making it into core
(it'll break several of my child themes and probably others that have been
using theme mods for years), I've been thinking of a possible solution for
theme authors who want to opt-into this functionality.
>
> Here's a quick bit of code that I was thinking about for theme authors.
It may need some tweaking (untested).
>
> {{{
> add_action( 'switch_theme', 'jt_switch_theme_update_mods' );
>
> function jt_switch_theme_update_mods() {
>
> if ( is_child_theme() && false === get_theme_mods() ) {
>
> $mods = get_option( 'theme_mods_' . get_option( 'template'
) );
>
> if ( false !== $mods ) {
>
> foreach ( (array) $mods as $mod => $value ) {
>
> if ( 'sidebars_widgets' !== $mod )
> set_theme_mod( $mod, $value );
> }
> }
> }
> }
> }}}
Had to modify this snippet slightly because of theme mods are already
exist when action is ran - there can by nav_menus and sidebars.
Make it work by putting following code in the parent theme:
{{{#!php
<?php
if( ! function_exists( 'prefix_switch_theme_update_mods' ) ) :
function prefix_switch_theme_update_mods( $prefix_new_theme ) {
if ( is_child_theme() ) {
$prefix_new_theme_mods = get_theme_mods();
//if is child theme and current theme mods are
empty - set theme mods from parent theme
if( empty( $prefix_new_theme_mods ) || 1 ===
count( $prefix_new_theme_mods ) || 2 === count( $prefix_new_theme_mods ) )
{
$prefix_mods = get_option( 'theme_mods_' .
get_template() );
if ( ! empty( $prefix_mods ) ) {
foreach ( (array) $prefix_mods as
$prefix_mod => $prefix_mod_value ) {
// if ( 'sidebars_widgets'
!== $prefix_mod )
set_theme_mod(
$prefix_mod, $prefix_mod_value );
}
}
}
}
}
endif;
add_action( 'after_switch_theme', 'prefix_switch_theme_update_mods' );
}}}
It checks if theme mods in child theme has lower than 2 options - parent
theme mods will be copied.
After switching to child theme this code can be deleted from parent theme.
Hope this will help someone like me )
--
Ticket URL: <https://core.trac.wordpress.org/ticket/27177#comment:29>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list