wordpress自定义组件时,规范的写法是需要对用户输入进行清理,可以利用sanitize_callback指定一个函数来进行数据的清理工作。这几天尝试修改wordpress的主题,想为主题增加一些自定义设置项。因为部分设置项需要设置一些默认值,为了测试主题初始化时默认值生效,所以会把options中的主题设置项删了又建,建了又删。
<?php
//delete_option('august_options');
function august_customize_register( $wp_customize ) {
if( !get_option( 'august_options' )) {
add_option( 'august_options' , array(
'lazyswitch' => '1',
'jquery' => 'https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery/3.6.0/jquery.min.js',
'lazyload' => 'https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery.lazyload/1.9.1/jquery.lazyload.min.js',
));
};
//全局设置部分
$wp_customize -> add_section('august_global', array(
'title' => '全局设置' ,
'priority' => 101,
));
$wp_customize->add_setting('august_options[lazyswitch]',array(
'transport' => 'postMessage',
'type' => 'option',
));
$wp_customize->add_control( 'august_options[lazyswitch]', array(
'label' => '主题图片懒加载',
'type' => 'radio',
'section' => 'august_global',
'choices' => array(
'1' => '开启',
'0' => '关闭',
)
));
//静态资源部分
$wp_customize -> add_section('august_static_resource', array(
'title' => '静态资源' ,
'priority' => 102,
));
$wp_customize->add_setting('august_options[jquery]',array(
'transport' => 'postMessage',
'type' => 'option',
));
$wp_customize->add_control( 'august_options[jquery]', array(
'label' => '主题jquery包',
'type' => 'textarea',
'section' => 'august_static_resource',
));
$wp_customize->add_setting('august_options[lazyload]',array(
'transport' => 'postMessage',
'type' => 'option',
));
$wp_customize->add_control( 'august_options[lazyload]', array(
'label' => '图片lazyload库',
'type' => 'textarea',
'section' => 'august_static_resource',
));
}
add_action( 'customize_register', 'august_customize_register' );
?>
起初是简单粗暴的在删除选项前加了注释,需要删除的时候就去掉注释,需要保留选项的时候就重新添加注释。来回拉扯次数多了,感觉略有些繁琐。
delete_option('august_options');
于是考虑临时增加一个删除全部设置项的选项,选择为真时删除所有设置项,选择为假时则不做改动。这样就不用来回修改注释了。
<?php
function august_customize_register( $wp_customize ) {
if( !get_option( 'august_options' )) {
add_option( 'august_options' , array(
'lazyswitch' => '1',
'jquery' => 'https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery/3.6.0/jquery.min.js',
'lazyload' => 'https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery.lazyload/1.9.1/jquery.lazyload.min.js',
));
};
//全局设置部分
$wp_customize -> add_section('august_global', array(
'title' => '全局设置' ,
'priority' => 101,
));
$wp_customize->add_setting('august_options[lazyswitch]',array(
'transport' => 'postMessage',
'type' => 'option',
));
$wp_customize->add_control( 'august_options[lazyswitch]', array(
'label' => '主题图片懒加载',
'type' => 'radio',
'section' => 'august_global',
'choices' => array(
'1' => '开启',
'0' => '关闭',
)
));
//静态资源部分
$wp_customize -> add_section('august_static_resource', array(
'title' => '静态资源' ,
'priority' => 102,
));
$wp_customize->add_setting('august_options[jquery]',array(
'transport' => 'postMessage',
'type' => 'option',
));
$wp_customize->add_control( 'august_options[jquery]', array(
'label' => '主题jquery包',
'type' => 'textarea',
'section' => 'august_static_resource',
));
$wp_customize->add_setting('august_options[lazyload]',array(
'transport' => 'postMessage',
'type' => 'option',
));
$wp_customize->add_control( 'august_options[lazyload]', array(
'label' => '图片lazyload库',
'type' => 'textarea',
'section' => 'august_static_resource',
));
//其它设置部分
$wp_customize -> add_section('august_others', array(
'title' => '其它设置' ,
'priority' => 103,
));
$wp_customize->add_setting('august_options[resetswitch]',array(
'transport' => 'postMessage',
'type' => 'option',
'sanitize_callback' => 'august_reset_options',
));
$wp_customize->add_control( 'august_options[resetswitch]', array(
'label' => '重置所有选项',
'type' => 'radio',
'section' => 'august_others',
'choices' => array(
'1' => '开启',
'0' => '关闭',
)
));
}
function august_reset_options($input){
if($input=='1'){
delete_option('august_options');
}
}
add_action( 'customize_register', 'august_customize_register' );
?>
于是删除了注释部分的语句,增加了一个清理函数,当然这个清理函数是个不务正业的,仅判断单选框的值,为真时则执行删除设置项的操作。