Customizer preview doesn’t update get_option() value properly

I have a setting in index.php

if(get_option('cb2_blog_home')){
    cb_get_blog_links();
}

This code allows the home page blog index to be turned on and off. The cb_get_blog_links() function draws out the list of blog post summaries.

And, in the customizer, I have a checkbox to allow this value cb2_blog_home to be set. However, it has no effect. Its as if the option is always true.

Any ideas why?

Inside that function, there are several variables that I’m also setting with the customizer. Those all work perfectly.

Here’s the full customizer code. Everything fires perfectly except the home blog setting. The only thing I can come up with is that one is a function and the others are variables within the function.

function cb_customize_register($wp_customize){

        $wp_customize->add_section('cb_customizer_blog', array(
                'title'    => __('Theme Blog Settings', 'Theme'),
                'priority' => 120,
        ));

                //Show & Hide Blog Links on Home
                $wp_customize->add_setting('cb2_blog_home', array(
                        'default'        => '',
                        'capability'     => 'edit_theme_options',
                        'type'           => 'option',
                ));

                $wp_customize->add_control('cb2_blog_home', array(
                        'label'      => __('Enable Home Blog', 'Theme'),
                        'section'    => 'cb_customizer_blog',
                        'settings'   => 'cb2_blog_home',
                        'type'   => 'checkbox',
                        'priority' => 125,
                ));

                //Home Blog Post Count
                $wp_customize->add_setting('cb2_blog_home_count', array(
                        'default'        => '3',
                        'capability'     => 'edit_theme_options',
                        'type'           => 'option',
                ));

                $wp_customize->add_control('cb2_blog_home_count', array(
                        'label'      => __('Home Blog Post Count', 'Theme'),
                        'section'    => 'cb_customizer_blog',
                        'settings'   => 'cb2_blog_home_count',
                        'priority' => 130,
                ));


                //Show & Hide Home Blog Title Heading
                $wp_customize->add_setting('cb2_blog_home_title_hide', array(
                        'default'        => '',
                        'capability'     => 'edit_theme_options',
                        'type'           => 'option',
                ));

                $wp_customize->add_control('cb2_blog_home_title_hide', array(
                        'label'      => __('Hide Home Blog Title', 'Theme'),
                        'section'    => 'cb_customizer_blog',
                        'settings'   => 'cb2_blog_home_title_hide',
                        'type'   => 'checkbox',
                        'priority' => 135,
                ));

                //Show & Hide Home Blog Author Byline
                $wp_customize->add_setting('cb2_blog_index_author', array(
                        'default'        => '',
                        'capability'     => 'edit_theme_options',
                        'type'           => 'option',
                ));

                $wp_customize->add_control('cb2_blog_index_author', array(
                        'label'      => __('Hide Blog Author Byline', 'Theme'),
                        'section'    => 'cb_customizer_blog',
                        'settings'   => 'cb2_blog_index_author',
                        'type'   => 'checkbox',
                        'priority' => 140,
                ));

            //Show & Hide Home Blog Date
            $wp_customize->add_setting('cb2_blog_index_date', array(
                        'default'        => '',
                        'capability'     => 'edit_theme_options',
                        'type'           => 'option',
                ));

                $wp_customize->add_control('cb2_blog_index_date', array(
                        'label'      => __('Hide Blog Date', 'Theme'),
                        'section'    => 'cb_customizer_blog',
                        'settings'   => 'cb2_blog_index_date',
                        'type'   => 'checkbox',
                        'priority' => 145,
                ));

}

add_action('customize_register', 'cb_customize_register');

1 Answer
1

You use the type option to store your settings.

Customizer doe not store your default values in database until user click on save.

@see https://wordpress.stackexchange.com/a/247164/52167

Leave a Comment