Customizer image control default value showing in customizer but not on frontend
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
- In Customizer window preview
- In Front End ( Checked in Incognito window too )
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:
- Why the default value is accessible in
Customizer Preview window
? ( As per the above code snippet.) - Is
default
parameter for the controlWP_Customize_Image_Control
is useful?
Leave an answer