Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have been trying to add a checkbox into the theme customizer. I have a checkbox added using this code in my functions.php

function theme_customizer_register_checkbox($wp_customize) {

$wp_customize->add_section( 'savior_global_options', array(
    'title'          => 'Global Options',
) );

$wp_customize->add_setting( 'show_supporters', array(
    'default'        => true,
    'type'           => 'option',
    'capability'     => 'edit_theme_options' )
);

$wp_customize->add_control( 'display_supporters', array(
    'settings' => 'show_supporters',
    'label'    => 'Show supporters section',
    'section'  => 'savior_global_options',
    'type'     => 'checkbox',
) );
 }
 add_action( 'customize_register', 'theme_customizer_register_checkbox' );

Once I got the checkbox working. I found that when I checked the box, it wouldn't change state. It always stayed checked. I did some searching and found post on this site that was solved but didn't leave an answer. The post led me to think that I needed to change the value in my $wp_customize->add_control() function to a unique value. So I changed it to display_supporters. This solved my issue but now I can't get the value of the checkbox. When I dump this variable,

$supporters = get_theme_mod('display_supporters);

I get bool(false)

and it stays at that. no matter what I do to the checkbox. What am I missing?

share|improve this question
    
Please stop inventing tags. Thanks. –  toscho Feb 27 at 4:56
1  
Total noob, but does this article help at all? –  Wrought_steel Feb 27 at 6:52
    
Yes, I got it with the code in the article. –  Jamie Feb 27 at 23:49
add comment

2 Answers 2

I'm pretty sure your theme modification setting isn't displaying as you're saving it as an option. You're setting it to save as an option in the $wp_customize->add_setting method with the 'type' => 'option' argument.

As it's an option you could retrieve it using get_option( 'show_supporters' ) instead. Note that 'show_supporters' is the name of the setting you've created not display_supporters.

However unless your theme already has a settings panel it's probably best to store it in your theme's modification settings. You can then use get_theme_mod( 'show_supporters' ) to get your value. Otto on Wordpress has written a good article about replacing option pages with the theme customizer.

I could only reproduce the problem with the checkbox not unchecking if the id of the add_setting and add_control methods did not match. I've included the code that worked for me below.

$wp_customize->add_setting( 'show_supporters', array(
    'default'        => true,
    'capability'     => 'edit_theme_options'
) );

$wp_customize->add_control( 'show_supporters', array(
    'settings' => 'show_supporters',
    'label'    => 'Show supporters section',
    'section'  => 'savior_global_options',
    'type'     => 'checkbox'
) );
share|improve this answer
    
the checkbox does allow me to check and uncheck it but the options still doesn't work. Under this code, I get this string(0) "" –  Jamie Feb 27 at 23:44
    
Does the value of the checkbox stay when you refresh the theme customizer? Are you using get_theme_mod( 'show_supporters' ) to show the setting? You could also have a look at the wp_options table in your database to see what's going on. If you save your setting as an option it will be called 'show_supporters'. If you save it using the theme settings it will be called theme_mods_themename and will contain a serialised array of your settings. –  Dylan Feb 28 at 0:14
    
I got it. Thanks for you help Dylan. It did help me look at the code a bit different –  Jamie Feb 28 at 0:26
add comment
up vote 0 down vote accepted

I was able to solve this. Here is the code that got the checkbox working.

function theme_customizer_register_checkbox($wp_customize) {

$wp_customize->add_section( 'global_options', array(
    'title'          => 'Global Options',
) );

$wp_customize->add_setting( 'show_supporters', array(
'default'        => true,
'transport'  =>  'postMessage'
 ) );

$wp_customize->add_control(
'show_supporters',
array(
    'section'   => 'global_options',
    'label'     => 'Show supporters section?',
    'type'      => 'checkbox'
     )
 );
 }
 add_action( 'customize_register', 'theme_customizer_register_checkbox' );

Then I checked for the value in the front end like so

if(true === get_theme_mod('show_supporters')){ do something here }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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