Last active
September 23, 2016 08:42
-
-
Save Netzberufler/20fc8697b48b4a7b5baf3f0b0341d9d6 to your computer and use it in GitHub Desktop.
Sanitization callback for select and radio type Customizer controls.
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 | |
/** | |
* Sanitization callback for 'select' and 'radio' type controls. | |
* | |
* @copyright Copyright (c) 2015, WordPress Theme Review Team, https://github.com/WPTRT/code-examples | |
* @see sanitize_key() https://developer.wordpress.org/reference/functions/sanitize_key/ | |
* @see $wp_customize->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 theme_slug_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 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment