使用的问题remove_theme_mods
用于在自定义程序中显示默认值的是
自定义程序是一个预览,您可以不保存就退出,过滤单个theme\\u mod,但不过滤整个theme\\u mod数组,theme\\u mod包括菜单和小部件我还想要一个重置按钮,但我选择了创建一个预设控件,其中一个预设是“默认值”。这种方式使用select
, 因此,按钮不工作没有问题(因为bind
用于值更改,按钮不更改其值)。
诀窍是使用ajax检索所选预设,然后循环javascript中的值,将它们分配给设置,以便这些更改将触发预览的刷新。我的代码包括过滤器,以便子主题可以添加更多选项和预设。预设可以是可用选项的子集。
这里是PHP for the Preset control (只是个普通人select
, 但无设置控件):
$wp_customize->add_control( \'option_presets\', array(
\'label\' => __( \'Use preset theme options\', \'mytheme\' ),
\'description\' => __( \'Theme options will be set to the preset values.\', \'mytheme\' ),
\'section\' => \'mytheme_section\',
\'settings\' => array(),
\'type\' => \'select\',
\'capability\' => \'edit_theme_options\',
\'choices\' => mytheme_option_presets_choices(),
) );
这是
rest of the PHP functions.
/**
* Supply list of choices for option presets.
*/
function mytheme_option_presets_choices() {
return apply_filters( \'mytheme_option_presets_choices\', array(
\'none\' => __( \'Select preset\', \'mytheme\' ),
\'defaults\' => __( \'Defaults\', \'mytheme\' ),
\'dark\' => __( \'Dark\', \'mytheme\' ),
) );
}
/**
* Sanitize an option preset choice.
*/
function mytheme_sanitize_option_presets_choice( $input ) {
$valid = mytheme_option_presets_choices();
return array_key_exists( $input, $valid ) ? $input : \'none\';
}
/**
* Get the preset values for the chosen option preset.
*/
function mytheme_option_preset( $which ) {
$values = array();
if ( \'defaults\' === $which ) {
$values = mytheme_default_values();
}
if ( \'dark\' === $which ) {
$values = array(
\'body_textcolor\' => \'#f9f7f7\',
\'background_color\' => \'#444244\',
\'header_textcolor\' => \'#bf9a07\',
\'area_classes\' => array(
\'sidebar\' => \'semi-black\',
\'widgets\' => \'box\',
),
);
}
return apply_filters( \'mytheme_option_preset\', $values, $which );
}
/**
* Add a nonce for Customizer for option presets.
*/
function mytheme_refresh_nonces( $nonces ) {
$nonces[\'mytheme-customize-presets\'] = wp_create_nonce( \'mytheme-customize-presets\' );
return $nonces;
}
add_filter( \'customize_refresh_nonces\', \'mytheme_refresh_nonces\' );
/**
* Ajax handler for supplying option preset values.
*/
function mytheme_ajax_option_preset_values() {
check_ajax_referer( \'mytheme-customize-presets\', \'option_presets_nonce\' );
if ( ! current_user_can( \'edit_theme_options\' ) ) {
wp_die( -1 );
}
if ( empty( $_POST[\'option_preset\'] ) ) {
wp_send_json_error( \'mytheme_missing_preset_parameter\' );
}
$preset = sanitize_text_field( wp_unslash( $_POST[\'option_preset\'] ) );
$values = mytheme_option_preset( $preset );
if ( empty( $values ) ) {
wp_send_json_error( array( \'message\' => __( \'No preset found.\', \'mytheme\' ) ) );
}
else { // Flatten the array.
foreach ($values as $key => $avalue) {
if ( is_array( $avalue ) ) {
unset( $values[$key] );
foreach ($avalue as $subkey => $subvalue) {
$values[$key . \'[\' . $subkey . \']\'] = $subvalue;
}
}
}
wp_send_json_success( array( \'values\' => $values ) );
}
}
add_action( \'wp_ajax_mytheme_option_preset\', \'mytheme_ajax_option_preset_values\' );
然后只是
little bit of Javascript 发出ajax请求。这已在上排队
\'customize_controls_enqueue_scripts\'
行动
(我没有显示错误消息。)wp.customize.control( \'option_presets\', function( control ) {
control.element = new wp.customize.Element( control.container.find( \'select\' ) );
control.element.bind( function( preset ) {
var request = wp.ajax.post( \'mytheme_option_preset\', {
option_presets_nonce: wp.customize.settings.nonce[\'mytheme-customize-presets\'],
wp_customize: \'on\',
customize_theme: wp.customize.settings.theme.stylesheet,
option_preset: preset
} );
request.done( function( response ) {
_.each( response.values, function( value, id ) {
var setting = wp.customize( id );
if ( setting ) {
setting.set( value );
}
} );
} );
} );
} );