我有以下自定义控件/设置来控制图像的不透明度:
// Image Opacity
$wp_customize->add_setting( \'image_opacity\', array(
\'default\' => \'1.000\',
\'sanitize_callback\' => \'my_theme_sanitize_number_range\'
) );
$wp_customize->add_control( \'image_opacity\', array(
\'label\' => __( \'Image Opacity\', \'my-theme\' ),
\'type\' => \'range\',
\'input_attrs\' => array(
\'min\' => 0,
\'max\' => 1.000,
\'step\' => 0.001,
),
) );
以及以下清理功能:
// Sanitize Number Range
function my_theme_sanitize_number_range( $number, $setting ) {
$number = absint( $number );
$atts = $setting->manager->get_control( $setting->id )->input_attrs;
$min = ( isset( $atts[\'min\'] ) ? $atts[\'min\'] : $number );
$max = ( isset( $atts[\'max\'] ) ? $atts[\'max\'] : $number );
$step = ( isset( $atts[\'step\'] ) ? $atts[\'step\'] : 0.001 );
return ( $min <= $number && $number <= $max && is_int( $number / $step ) ? $number : $setting->default );
}
它可以很好地处理整数,比如
\'max\' => 1,
和
\'step\' => 1
但我想让步骤变成三个小数点,比如
\'step\' => 0.001
, 这是行不通的。
我如何做到这一点?
最合适的回答,由SO网友:Kaperto 整理而成
您需要删除的呼叫absint
开始时。您应该进行更多的调试以避免此错误。
之后,部门$number / $step
始终返回浮点。然后,要对值进行四舍五入,请使用类似的方法:
$number = floor($number / $atts[\'step\']) * $atts[\'step\'];
return ( $min <= $number && $number <= $max ) ? $number : $setting->default;