How do I check if user input in a field in the customizer is a number?

You are right. A couple of fixes/suggestions, though.

  • You should use absint as a sanitization callback.
  • Also, you don’t have to explicitly use the WP_Customize_Control class. The number field can be created with the regular syntax you use for the text field with an exception of the type and additional attributes.
  • Another thing is that you don’t have to define the refresh transport method since it is used by default.

So the refined snippet should look something like this:

$wp_customize->add_setting( 'posts_number_home' , array(
    'default'           => 10,
    'sanitize_callback' => 'absint',
) );

$wp_customize->add_control( 'posts_number_home', array(
    'label'    => __( 'Number of posts to display on homepage', 'mytheme' ), 
    'section'  => 'mytheme_options',
    'settings' => 'posts_number_home',
    'type'     => 'number',
    'priority' => 10,
    'input_attrs' => array(
        'min' => 1,
        'max' => 10
    )
) );

Edit 07/01/2016: Using intval is not correct because Customizer secretly passes the second object parameter to sanitization callback, while intval expects an optional integer. So the best way to sanitize non-negative integer is absint.

Leave a Comment