catchresponsive_sanitize_number_range

Number Range sanitization callback example.

Description

(int|string) catchresponsive_sanitize_number_range( (number) $number, (WP_Customize_Setting) $setting ); 

- Sanitization: number_range - Control: number, tel

Sanitization callback for number or tel type text inputs. This callback sanitizes '$number' as an absolute integer within a defined min-max range.

Returns (int|string)

The number, if it is zero or greater and falls within the defined range; otherwise, the setting default.

Parameters (2)

0. $number (number)
Number to check within the numeric range defined by the setting.
1. $setting (WP_Customize_Setting)
Setting instance.

Usage

  1. if ( !function_exists( 'catchresponsive_sanitize_number_range' ) ) { 
  2. require_once '/inc/customizer-includes/catchresponsive-customizer-sanitize-functions.php'; 
  3.  
  4. // Number to check within the numeric range defined by the setting. 
  5. $number = null; 
  6.  
  7. // Setting instance. 
  8. $setting = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = catchresponsive_sanitize_number_range($number, $setting); 
  12.  

Defined (1)

The function is defined in the following location(s).

/inc/customizer-includes/catchresponsive-customizer-sanitize-functions.php  
  1. function catchresponsive_sanitize_number_range( $number, $setting ) { 
  2.  
  3. // Ensure input is an absolute integer. 
  4. $number = absint( $number ); 
  5.  
  6. // Get the input attributes associated with the setting. 
  7. $atts = $setting->manager->get_control( $setting->id )->input_attrs; 
  8.  
  9. // Get minimum number in the range. 
  10. $min = ( isset( $atts['min'] ) ? $atts['min'] : $number ); 
  11.  
  12. // Get maximum number in the range. 
  13. $max = ( isset( $atts['max'] ) ? $atts['max'] : $number ); 
  14.  
  15. // Get step. 
  16. $step = ( isset( $atts['step'] ) ? $atts['step'] : 1 ); 
  17.  
  18. // If the number is within the valid range, return it; otherwise, return the default 
  19. return ( $min <= $number && $number <= $max && is_int( $number / $step ) ? $number : $setting->default );