get_control() https://developer.wordpress.org/reference/classes/wp_customize_manager/get_control/ * * @param string $input Slug to sanitize. * @param WP_Customize_Setting $setting Setting instance. * @return string Sanitized slug if it is a valid choice; otherwise, the setting default. */ function bam_sanitize_select( $input, $setting ) { // Ensure input is a slug. $input = sanitize_key( $input ); // Get list of choices from the control associated with the setting. $choices = $setting->manager->get_control( $setting->id )->choices; // If the input is a valid key, return it; otherwise, return the default. return ( array_key_exists( $input, $choices ) ? $input : $setting->default ); } /** * Number sanitization. * * - Sanitization: number_absint * - Control: number * * Sanitization callback for 'number' type text inputs. This callback sanitizes `$number` * as an absolute integer (whole number, zero or greater). * * NOTE: absint() can be passed directly as `$wp_customize->add_setting()` 'sanitize_callback'. * It is wrapped in a callback here merely for example purposes. * * @see absint() https://developer.wordpress.org/reference/functions/absint/ * * @param int $number Number to sanitize. * @param WP_Customize_Setting $setting Setting instance. * @return int Sanitized number; otherwise, the setting default. */ function bam_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 ); } /** * Check if the given value is a number or blank. */ function bam_sanitize_number_blank( $number, $setting ) { // Ensure $number is an absolute integer (whole number, zero or greater). $number = absint( $number ); if ( $number >= 0 ) { return $number; } else { return $setting->default; } } /** * Number Range sanitization. * * - 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. * * @see absint() https://developer.wordpress.org/reference/functions/absint/ * * @param int $number Number to check within the numeric range defined by the setting. * @param WP_Customize_Setting $setting Setting instance. * @return int|string The number, if it is zero or greater and falls within the defined range; otherwise, * the setting default. */ function bam_sanitize_number_range( $number, $setting ) { // Ensure input is an absolute integer. $number = absint( $number ); // Get the input attributes associated with the setting. $atts = $setting->manager->get_control( $setting->id )->input_attrs; // Get minimum number in the range. $min = ( isset( $atts['min'] ) ? $atts['min'] : $number ); // Get maximum number in the range. $max = ( isset( $atts['max'] ) ? $atts['max'] : $number ); // Get step. $step = ( isset( $atts['step'] ) ? $atts['step'] : 1 ); // If the number is within the valid range, return it; otherwise, return the default return ( $min <= $number && $number <= $max && is_int( $number / $step ) ? $number : $setting->default ); } function bam_sanitize_slider_number_input( $number, $setting ) { // Ensure input is a number. $number = (float)$number ; // Get the input attributes associated with the setting. $choices = $setting->manager->get_control( $setting->id )->choices; // Get minimum number in the range. $min = ( isset( $choices['min'] ) ? $choices['min'] : $number ); // Get maximum number in the range. $max = ( isset( $choices['max'] ) ? $choices['max'] : $number ); // Get step. $step = ( isset( $choices['step'] ) ? $choices['step'] : 1 ); if ( $number <= $min ) { $number = $min; } elseif ( $number >= $max ) { $number = $max; } // If the number is within the valid range, return it; otherwise, return the default return ( is_numeric( $number / $step ) ? $number : $setting->default ); } /** * HEX Color sanitization. * * - Sanitization: hex_color * - Control: text, WP_Customize_Color_Control * * Note: sanitize_hex_color_no_hash() can also be used here, depending on whether * or not the hash prefix should be stored/retrieved with the hex color value. * * @see sanitize_hex_color() https://developer.wordpress.org/reference/functions/sanitize_hex_color/ * @link sanitize_hex_color_no_hash() https://developer.wordpress.org/reference/functions/sanitize_hex_color_no_hash/ * * @param string $hex_color HEX color to sanitize. * @param WP_Customize_Setting $setting Setting instance. * @return string The sanitized hex color if not null; otherwise, the setting default. */ function bam_sanitize_hex_color( $hex_color, $setting ) { // Sanitize $input as a hex value without the hash prefix. $hex_color = sanitize_hex_color( $hex_color ); // If $input is a valid hex value, return it; otherwise, return the default. return ( ! is_null( $hex_color ) ? $hex_color : $setting->default ); } /** * Checkbox sanitization callback example. * * Sanitization callback for 'checkbox' type controls. This callback sanitizes `$checked` * as a boolean value, either TRUE or FALSE. * * @param bool $checked Whether the checkbox is checked. * @return bool Whether the checkbox is checked. */ function bam_sanitize_checkbox( $checked ) { // Boolean check. return ( ( isset( $checked ) && true == $checked ) ? true : false ); } /** * Sanitization callback of Multiple Checkboxes Control */ function bam_sanitize_multiple_checkboxes( $values ) { $multi_values = !is_array( $values ) ? explode( ',', $values ) : $values; return !empty( $multi_values ) ? array_map( 'sanitize_text_field', $multi_values ) : array(); } /** * HTML sanitization callback. * * - Sanitization: html * - Control: text, textarea * * Sanitization callback for 'html' type text inputs. This callback sanitizes `$html` * for HTML allowable in posts. * * NOTE: wp_filter_post_kses() can be passed directly as `$wp_customize->add_setting()` * 'sanitize_callback'. It is wrapped in a callback here merely for example purposes. * * @see wp_filter_post_kses() https://developer.wordpress.org/reference/functions/wp_filter_post_kses/ * * @param string $html HTML to sanitize. * @return string Sanitized HTML. */ function bam_sanitize_html( $html ) { return wp_filter_post_kses( $html ); } /** * URL sanitization. * * - Sanitization: url * - Control: text, url * * Sanitization callback for 'url' type text inputs. This callback sanitizes `$url` as a valid URL. * * NOTE: esc_url_raw() can be passed directly as `$wp_customize->add_setting()` 'sanitize_callback'. * It is wrapped in a callback here merely for example purposes. * * @see esc_url_raw() https://developer.wordpress.org/reference/functions/esc_url_raw/ * * @param string $url URL to sanitize. * @return string Sanitized URL. */ function bam_sanitize_url( $url ) { return esc_url_raw( $url ); } /** * Email sanitization * * - Sanitization: email * - Control: text * * Sanitization callback for 'email' type text controls. This callback sanitizes `$email` * as a valid email address. * * @see sanitize_email() https://developer.wordpress.org/reference/functions/sanitize_key/ * @link sanitize_email() https://codex.wordpress.org/Function_Reference/sanitize_email * * @param string $email Email address to sanitize. * @param WP_Customize_Setting $setting Setting instance. * @return string The sanitized email if not null; otherwise, the setting default. */ function bam_sanitize_email( $email, $setting ) { // Strips out all characters that are not allowable in an email address. $email = sanitize_email( $email ); // If $email is a valid email, return it; otherwise, return the default. return ( ! is_null( $email ) ? $email : $setting->default ); }