-1

How to sanitize input control with "type" => "number". It would better if somebody actually explain in detail sanitize_callback on customizer.

4

Add a setting, specifying the sanitize_callback:

$wp_customize->add_setting(
    'my_input',
    array(
        'default' => '100.00',
        'sanitize_callback' => 'sanitize_float',
    )
);

Add the control:

$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',
          ),
    )
);

Create a function to perform the sanitisation - filter_var should return the sanitised float or false:

function sanitize_float( $input ) {
    return filter_var($input, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
}

This is untested, but should get you started - it could, of course, be adapted to sanitize an integer.

[Adapted from http://themefoundation.com/wordpress-theme-customizer/]

1
  • I had tried using 'floatval' but was getting a null response. After using this 'sanitize_float' function I started getting valid responses (when changing a value in a customizer control, which used this function to sanitize the value) Jun 3 '20 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.