Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

$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.

like image 305
user3452005 Avatar asked Apr 03 '14 13:04

user3452005


Video Answer


2 Answers

$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.

like image 168
Bican M. Valeriu Avatar answered Oct 30 '22 15:10

Bican M. Valeriu


if the setting 'my_theme_settings[social_facebook]' checkbox is unchecked:

<?php if( get_theme_mod( 'my_theme_settings[social_facebook]' ) == '') { ?>

 //if setting is unchecked content here will be shown

<?php } // end if ?>

For full article see: http://themefoundation.com/wordpress-theme-customizer

like image 24
Kieran Hunter Avatar answered Oct 30 '22 13:10

Kieran Hunter