输入类型编号的定制器SANITIZE_CALLBACK

时间:2016-05-06 作者:Ashish

如何使用清理输入控件"type" => "number". 如果有人真的详细解释一下customizer上的sanitize\\u回调,那就更好了。

1 个回复
SO网友:mistertaylor

添加设置,指定sanitize_callback:

$wp_customize->add_setting(
    \'my_input\',
    array(
        \'default\' => \'100.00\',
        \'sanitize_callback\' => \'sanitize_float\',
    )
);
添加控件:

$wp_customize->add_control(
    \'my_input\',
    array(
        \'label\' => \'Please enter a number:\',
        \'section\' => \'my_section\',
        \'type\' => \'number\',
        \'input_attrs\' => array(
            \'min\' => \'0.01\', \'step\' => \'0.01\', \'max\' => \'10000\',
          ),
    )
);
创建执行消毒的功能-filter_var 应返回经过消毒的浮子或false:

function sanitize_float( $input ) {
    return filter_var($input, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
}
这是未经测试的,但应该让您开始-当然,它可以调整为清理整数。

[改编自http://themefoundation.com/wordpress-theme-customizer/]

相关推荐

Need a help on sanitization

我在评论中有一个主题,我确实在像这样的少数情况下添加了“type”=>“select”部分。$wp_customize->add_setting( \'personal_lite_post_link\', array( \'default\' => \'enable\', \'sanitize_callback\' => \'personal_lite_select_callback\' ) ); $w