Customizer: How to Sanitize a Decimal Number Range

I have the following Customizer control/settings to control an image’s opacity:

// 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,
),
) );

and the following sanitize function:

// 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 );
}

It works fine for whole numbers, like if ‘max’ => 1, and ‘step’ => 1 but I want the steps to three decimal points like ‘step’ => 0.001, which doesn’t work.

How do I accomplish this?

Read more here:: Customizer: How to Sanitize a Decimal Number Range

Leave a Reply

Your email address will not be published. Required fields are marked *