How to get the values of WordPress customize checkboxes

I can’t figure out how to get the value – whether they are checked or not – from checkboxes in the WP customize manager.

This is the code in functions.php:

Read More
$wp_customize->add_setting('social_facebook', array(
    'type'       => 'option',
));

$wp_customize->add_control(
    new WP_Customize_Control(
        $wp_customize,
        'social_facebook',
        array(
            'label'          => __( 'Facebook', 'theme_name' ),
            'section'        => 'social-icons',
            'settings'       => 'social_facebook',
            'type'           => 'checkbox',
        )
    )
);

And this is how I try to get the value:

<?php
$facebook = get_theme_mod('social_facebook');
if ($facebook != ''){?>
<style>
    .facebook {display:inline!important;}
</style>
<?php }
?>

They values of the checkboxes are either “” (empty) or “1”, so the system registers the checking of them. However, I don’t know how to get the value through the get_theme_mod approach. Also, they don’t have any name values, so I can’t get the value through the usual way either.

Related posts

Leave a Reply

6 comments

  1. $sticky_mod = get_theme_mod( 'wca_header_section_sticky' ) == '1' ? 'sticky' : '';
    

    This is an example in my case – if that option is checked, it will echo a “sticky” class in my template.

  2. Try use and customize this (tested, in functions.php:

    function mytheme_customize_register( $wp_customize ){
      $wp_customize->add_section(
      // ID
      'layout_section',
      // Arguments array
      array(
        'title' => __( 'Layout', 'my_theme' ),
        'capability' => 'edit_theme_options',
        'description' => __( 'social needs ;)', 'my_theme' )
      )
     );
    
     $wp_customize->add_setting(
      // ID
      'my_theme_settings[social_facebook]',
      // Arguments array
      array('type' => 'option')
      );
    
     $wp_customize->add_control(
      // ID
      'layout_control',
    array(
      'type' => 'checkbox',
      'label' => __( 'Facebook', 'my_theme' ),
      'section' => 'layout_section',
      // This last one must match setting ID from above
      'settings' => 'my_theme_settings[social_facebook]'
     )
     );
    }
    
    add_action( 'customize_register', 'mytheme_customize_register' );
    

    to read in template

    $my_theme_settings = get_option( 'my_theme_settings' );
    echo $my_theme_settings['social_facebook'];
    
  3. the problem is in the WP_Customize_Setting::value() it expect to return false
    to uncheck the checkbox (or leave the checkbox unchecked) while some program will return ‘0’ or ”.

    In my case I have to extend the WP_Customize_Setting and override the value() method to force returning boolean.

    <?php
    class ForceBooleanSettings
    extends WP_Customize_Setting {
    
      public function value() {
        return (bool) parent::value();
      }
    }
    
    // Example on using this extend class
    $customizer->add_setting(new ForceBooleanSettings(
       $customizer, 
       'myuniquekey', 
       array(
        'default' => false,
        'transport' => 'refresh',
    )));
    
    ?>
    
  4. Here is my working solution:

    function theme_name_custom_settings( $wp_customize ) {
    
        $wp_customize->add_setting( 'social_facebook', array(
            'default'   => 1, // Set default value
            'sanitize_callback' => 'esc_attr', // Sanitize input
            )
        );
    
        $wp_customize->add_control( 
            new WP_Customize_Control(
                $wp_customize,
                'social_facebook', // Setting ID
                array(
                    'label'     => __( 'Facebook', 'theme_name' ),
                    'section'   => 'social_icons', // No hyphen
                    'settings'  => 'social_facebook', // Setting ID
                    'type'      => 'checkbox',
                )
            )
        );
    
    }
    
    add_action( 'customize_register', 'theme_name_custom_settings' );
    

    Then check it’s value (0 or 1) like so:

    if ( !get_theme_mod( 'social_facebook' ) ) { // false
        return;
    }
    
    // or
    
    if ( get_theme_mod( 'social_facebook' ) == 1 ) { // true
        return;
    }