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:
$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.
This is an example in my case – if that option is checked, it will echo a “sticky” class in my template.
if the setting ‘my_theme_settings[social_facebook]’ checkbox is unchecked:
For full article see: http://themefoundation.com/wordpress-theme-customizer
Try use and customize this (tested, in functions.php:
to read in template
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.
Here is my working solution:
Then check it’s value (0 or 1) like so: