I am building a plugin and I want to add some settings which rely on checkboxes. I know how to get data from the checkbox in PHP but got stuck in WordPress. Because we have no option to add the name attribute to the checkbox. for example, I am adding a checkbox in the customizer.

$wp_customize->add_setting('ion', array(
        'sanitize_callback' => 'ion_checkbox'
    $wp_customize->add_control(new WP_Customize_Control($wp_customize, 'ion', array(
        'label' => 'Check',
        'type' => 'checkbox',
        'section' => 'search_submit_section',
        'settings' => 'ion'

As we see there is no option to add name attribute. How we will know whether the user has checked the checkbox or not. I hope you understand my question.

2 Answers 2


It has been quite some time, but you can simply check if the mod value is true or false.

    if( get_theme_mod('ion') == true ){
       //your html

Sanitizing a checkbox is quite easy. Since the checkbox value is either true or false (1 or 0), you simply need to return one of these values. A simple function can be written that tests if the checkbox has been set, and if so, it returns 1 otherwise it returns 0.

 function mytheme_chkbox_sanitization( $input ) {
if ( true === $input ) {
    return 1;
 } else {
    return 0;

When creating the Setting for your Checkbox, pass the name of this function to the sanitize_callback argument.

 $wp_customize->add_setting( 'sample_default_checkbox',
    'default' => 0,
    'sanitize_callback' => 'mytheme_chkbox_sanitization',
  • i checked this code before on github but this is not the answer. please provide other example of sanitize callback function because that function i am not getting. Dec 4, 2018 at 14:12
  • I have edit it. Usually I use if statements that work fine. Are you sure you didn't miss something?
    – dado
    Dec 4, 2018 at 14:15
  • i am still not getting how will i use get_theme_mod because i am creating checkbox from here not anywhere in html. i think it will generate error Dec 4, 2018 at 14:26
  • Check other example of sanitize
    – dado
    Dec 4, 2018 at 14:32
  • @dado My lap shutdown. Now messaging from phone using other account. Yes I agree checkbox has either true or false value. So if user checks checkbox then I want to use add_action, how to do that Dec 4, 2018 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.