0

I'm using the Customizer API to enable/disable different sections of a website.

When I have the customizer window open it seems that get_theme_mod() is returning a value ok. However as soon as I save and close the customizer window, get_theme_mod() does not return anything.

I've seen some other questions about using type="option" and get_option() but I want to see if I can get this to work.

My settings look like this:

$wp_customize->add_setting( 'my_banner_setting', array(
    'default'           => '1',
    'sanitize_callback' => 'my_sanitize_checkbox',
) );

And then I get them like this:

if ( get_theme_mod( 'my_banner_setting' ) == 1 ) {

    get_template_part( 'sections/banner' );

}

Is there something I'm missing?

2

Looks like I was able to fix it by adding a default value at the end of get_theme_mod()

if (  get_theme_mod('my_banner_setting', 1) == 1 ) {

    // proceed

}

Not sure if there's another solution but this fixed it.

update

Looks like it was actually because my "1" was in quotes, removing the quote worked and I didn't have to declare the default in get_theme_mod() again.

$wp_customize->add_setting( 'my_banner_setting', array(
    'default'           => 1,
    'sanitize_callback' => 'my_sanitize_checkbox',
) );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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