Last active
November 21, 2019 14:44
-
-
Save them-es/a30a917a58a05d5baee40b0ce9b94c18 to your computer and use it in GitHub Desktop.
Multisite: Duplicate Theme Customizer values across network
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 | |
/** | |
* Duplicate specific Theme Customizer values across Multisite network | |
* | |
*/ | |
function my_theme_customize_multisite_duplicate() { | |
if ( is_multisite() ) { | |
// Get existing values from current blog as array | |
$duplicated_values = array( | |
'customizer_value_1', | |
'customizer_value_2', | |
'customizer_value_3', | |
); | |
foreach ( $duplicated_values as $value ) { | |
$duplicates[] = get_theme_mod( $value ); // Get duplicated theme_mod variables as array | |
} | |
$sites = get_sites(); // https://codex.wordpress.org/Function_Reference/get_sites | |
foreach ( $sites as $site ) { | |
$current_site = $site->blog_id; | |
// Exclude current blog | |
if ( get_current_blog_id() !== $current_site ) : | |
switch_to_blog( $current_site ); | |
// Duplicate previously defined values from other blog | |
foreach ( $duplicated_values as $key => $value ) { | |
set_theme_mod( $value, $duplicates[$key] ); // Set duplicated theme_mod variables from array | |
} | |
restore_current_blog(); | |
endif; | |
} | |
} | |
} | |
add_action( 'customize_save_after', 'my_theme_customize_multisite_duplicate', 99 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment