这很容易做到。您要做的是挂接到customize\\u save\\u after action挂钩-https://developer.wordpress.org/reference/hooks/customize_save_after/ - 并检查您的设置,并在此基础上更新另一个设置。示例:
add_action( \'customize_save_after\', function() {
// Get the setting you want to retrieve value from
$new_value_setting_1 = get_theme_mod( \'SETTING_ID\' );
// Update another setting
if ( $new_value_setting_1 ) {
set_theme_mod( \'SETTING_ID\', $new_value_setting_1 );
}
// Delete theme mod if setting is empty rather then storing an empty value
else {
remove_theme_mod( \'SETTING_ID\', $new_value_setting_1 );
}
} );
因此,无论何时更改自定义设置,都会更新其他设置。不幸的是,如果没有JS(据我所知),当设置更改时,在定制器中执行此操作是不可能的。如果您想使用PHP执行此操作,您应该在之后连接到customize\\u save\\u。
除非。。。如果要检查的设置使用刷新方法,因此在修改该设置时,自定义程序窗口会刷新,那么可以挂接到init来更改该值。。像这样:
add_action( \'init\', function() {
// Only needed in the customizer
if ( ! is_customize_preview() ) {
return;
}
// Get the setting you want to retrieve value from
$new_value_setting_1 = get_theme_mod( \'SETTING_ID\' );
// Update another setting
if ( $new_value_setting_1 ) {
set_theme_mod( \'SETTING_ID\', $new_value_setting_1 );
}
// Delete theme mod if setting is empty rather then storing an empty value
else {
remove_theme_mod( \'SETTING_ID\', $new_value_setting_1 );
}
} );
我希望这有帮助!