If you’re not using the WordPress Customizer for your themes yet, you should definitely start. Alex Mansfield has written an amazingly detailed guide on the WordPress Theme Customizer. Following this guide I went from being terrified of the customizer to adding all my necessary options in one night.
There was one thing however that just didn’t sit right with me and that was the sanitization of radio buttons and select lists. It just didn’t feel right having to type out my choices array twice, once for the control and once for the sanitize callback. If I needed to update my choices array in the future I would have to do it in two places increasing my chances for errors. On top of that I didn’t like returning a blank string because it’s not one of my options.
After some digging into the WordPress core I discovered that when WordPress calls your sanitize function it actually passes a second argument which is the setting object the sanitize callback belongs to. This is truly magical! It will allow us to to retrieve the default value for the setting and get the choices we already setup for our control. The best part is we can create one callback that we can use for all our radio buttons and select lists. Awesome, right? Let’s take a look at the code to do this.
function example_sanitize_choices( $input, $setting ) { global $wp_customize; $control = $wp_customize->get_control( $setting->id ); if ( array_key_exists( $input, $control->choices ) ) { return $input; } else { return $setting->default; } }
First we add a second parameter $setting to our callback function so we can access all the juicy information stored in the setting object like the id and default value. Moving inside our function we declare the global $wp_customize. This is important because it will allow us to access the control for our setting and its choices. Next we pass $setting->id as the argument to $wp_customize->get_control() to get our control that belongs to the setting. Now that we have the control we can check if our $input is valid against our choices for the control. If it’s valid we return the input if not we return the default value for the setting.
That’s it! Now you have one callback to handle the sanitizing of all your radio buttons and select lists.
Big thanks to Alex Mansfield for writing such a great guide on the customizer. I know it helped me get started with the theme customizer and it can definitely help you too. It even inspired me to write my first blog post.
Congratulations on your first blog post! I’m looking forward to updating my tutorial with your contribution when I get a change.
Thanks! I’ve been looking all over for a solution to this. I was sure there was a simple solution, and I’m glad I’ve found it. I’ve got about a dozen options that use a select field, so this makes it much easier.
I’m happy I could help out Richard!
Hey there,
Thanks for a great write up. I’m still confused.
I’m not sure how to use this function. Below is my code. Just trying to figure out how to get this done. I’m not sure where to place the function or what I might need to change.
// Mobile Nav Hide Right Arrow
function digistarter_sanitize_choices( $input, $setting ) {
global $wp_customize;
$control = $wp_customize->get_control( $setting->id );
if ( array_key_exists( $input, $control->choices ) ) {
return $input;
} else {
return $setting->default;
}
}
$wp_customize->add_setting(
‘digistarter_mobile_hide_arrow’,
array(
‘default’ => “No”
)
);
$wp_customize->add_control(
‘digistarter_mobile_hide_arrow’,
array(
‘section’ => ‘nav’,
‘label’ => ‘Mobile Navigation Hide Right Arrow’,
‘type’ => ‘radio’,
‘choices’ => array(“Yes”, “No”),
)
);
Hi Alex,
I am sorry I am slow replying to you but you need to add the function as your sanitize_callback when you add the setting.
$wp_customize->add_setting(
‘digistarter_mobile_hide_arrow’,
array(
‘default’ => “No”,
‘sanitize_callback’ => ‘example_sanitize_choices’
)
);
Awesome, I’m glad I came across this after reading Alex’s tutorial. Thanks for sharing!
I know you wrote this forever ago but I just wanted to say THANK YOU! I’ve literally spent the last 3 hours pulling my hair out, with everyone saying the same damned thing and none of it working the way I wanted. I KNEW there had to be a way to not have to write in the same data twice. This should definitely be a core WP function. It’s so simple! (Once you know it)
Thank you so much! I’ve read a lot of articles from reputable sources and I haven’t been able to find this. It’s even better than anything I’d hoped to find. This will save me a lot of time and frustration!
Hi,
First thank a lot for this. I have an issue which said:
Warning: array_key_exists() expects parameter 2 to be array, null given in
It’s like the $setting var doesn’t exist.
Is someone already faced this issue?
Can you show the code you are using that is producing this error?