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

The default value that you set on the image add_setting will only be applyed if there is a any option called ‘reset to default’ on the image control. This argument will not output any default value to the page.

The second argument of the function get_option( 'option_name', $default ). The $default parameter will not submit anything to the DB. It only returned if the option does not exists. e.g: when the user installed the theme, and the logo (or the option that displays anything on the page) must not be empty. But if he save the option, the option will exist on the db, even if empty. Then this default will not apply anymore. it works like a place holder.

If you want a default value, even if the option are saved and return a empty string, you can do this:

$option = get_option( 'option_name', $default )
echo ( empty( $option ) ? 'default' : $option );

The empty() function will check if the returned value are a empty string, or anything that represents a empty (boolean, integer, null, etc). You can read more here: http://php.net/manual/pt_BR/function.empty.php

This way, a default value will be ever applyed, if the option exists.

Note: its a best practice to use 'type' => 'theme_mod' when creating mods for themes, not 'type' => 'option'. If you omit this
arg, the default will be theme_mod.

Leave a Comment