ControlledChaos
10/25/2016 - 3:07 AM

Add a Number field to the WordPress Customizer.

Add a Number field to the WordPress Customizer.

$wp_customize->add_setting( 'themeslug_number_setting_id', array(
  'capability' => 'edit_theme_options',
  'sanitize_callback' => 'themeslug_sanitize_number_absint',
  'default' => 1,
) );

$wp_customize->add_control( 'themeslug_number_setting_id', array(
  'type' => 'number',
  'section' => 'custom_section', // Add a default or your own section
  'label' => __( 'Custom Number' ),
  'description' => __( 'This is a custom number.' ),
) );

function themeslug_sanitize_number_absint( $number, $setting ) {
  // Ensure $number is an absolute integer (whole number, zero or greater).
  $number = absint( $number );

  // If the input is an absolute integer, return it; otherwise, return the default
  return ( $number ? $number : $setting->default );
}