Customizer image control default value showing in customizer but not on frontend

Question

I use the WP_Customize_Image_Control to add the image.

But, The default value is accessible in Customizer only!!!
On front-end It return empty.

How to reproduce?

Copy below code snippet and paste in your themes functions.php.

Visit URL http://YOUR_SITE/wp-admin/customize.php?autofocus[section]=section-test_option to open section Test Section

add_action( 'customize_register', 'test_1234_customize_register' );
add_action( 'wp_head', 'test_1234_customizer_ouput_debug' );

function test_1234_customizer_ouput_debug() {

    // $options = get_theme_mod( 'this-is-the-test-option' );
    $options = get_option( 'this-is-the-test-option' );
    echo '<pre style="background: #fff;">Default Image URL: ';
    print_r( $options );
    echo '</pre>';
}

function test_1234_customize_register( $wp_customize ) {

    /**
     * Test Section
     */
    $wp_customize->add_section( 'section-test_option', array(
        'title' => __( 'Test Option', 'next' ),
    ) );

    /**
     * Test Option - 1
     */
    $wp_customize->add_setting( 'this-is-the-test-option', array(
        'default' => 'https://www.google.co.in/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png',
        'type'    => 'option',  // Comment this parameter to use 'get_theme_mod'
    ) );
    $wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'this-is-the-test-option', array(
        'section'        => 'section-test_option',
        'label'          => __( 'Test', 'next' ),
        'settings'       => 'this-is-the-test-option',
        'library_filter' => array( 'gif', 'jpg', 'jpeg', 'png', 'ico' ),
    ) ) );
}

Output

  1. In Customizer window preview

http://bsf.io/net4f

  1. In Front End ( Checked in Incognito window too )

http://bsf.io/u59cm


As per above example I’m able to use:
get_option( 'this-is-the-test-option', 'https://www.google.co.in/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png' ) to get default image.

But, It’ll be fail if I store the options in array. E.g.

$wp_customize->add_setting( 'this-is-the-test-option[option1]', array(
...

$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'this-is-the-test-option[option1]', array(
...

In above situation the best solution is merging the default values. I found the solution suggested by @westonruter in gist.


But, Questions are:

  1. Why the default value is accessible in Customizer Preview window? ( As per the above code snippet.)
  2. Is default parameter for the control WP_Customize_Image_Control is useful?
0
, , maheshwaghmare 3 years 2020-04-05T00:50:51-05:00 0 Answers 92 views 0

Leave an answer

Browse
Browse