Created
April 15, 2020 19:34
-
-
Save lkhedlund/8a8a8cfc83cd10d077d407287244b55c to your computer and use it in GitHub Desktop.
Sanitizer examples for customizer settings.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Sanitize Select fields | |
function sanitize_select($input, $setting) { | |
$input = sanitize_key($input); | |
$choices = $setting->manager->get_control($setting->id)->choices; | |
return (array_key_exists($input, $choices) ? $input : $setting->default); | |
} | |
// Sanitize Checkboxes | |
function sanitize_checkbox($input) { | |
return ( ( isset( $input ) && true == $input ) ? true : false ); | |
} | |
// Sanitize Range | |
function sanitize_range($input, $setting) { | |
// Ensure input is an absolute integer | |
$input = absint( $input ); | |
// Get the input attributes | |
// associated with the setting | |
$atts = $setting->manager->get_control( $setting->id )->input_attrs; | |
// Get min | |
$min = ( isset( $atts['min'] ) ? $atts['min'] : $input ); | |
// Get max | |
$max = ( isset( $atts['max'] ) ? $atts['max'] : $input ); | |
// Get Step | |
$step = ( isset( $atts['step'] ) ? $atts['step'] : 1 ); | |
// If the input is within the valid range, | |
// return it; otherwise, return the default | |
return ( $min <= $input && $input <= $max && is_int( $input / $step ) ? $input : $setting->default ); | |
} | |
// Sanitize Pages Dropdown | |
function sanitize_dropdown_pages($input, $setting) | |
{ | |
// Ensure $input is an absolute integer. | |
$page_id = absint( $input ); | |
// If $page_id is an ID of a published page, return it; otherwise, return the default. | |
return ( 'publish' == get_post_status( $page_id ) ? $page_id : $setting->default ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment