How to check if a Customizer setting is set?

I’m trying to setup a function that writes a meta tag into the header of my site.

Namely, I’ve set up a color chooser input like this:

function customizer_options($wp_customize) {

$wp_customize -> add_setting ( ‘chrome_theme’, array( ‘default’ => ” ) );
$wp_customize -> add_control ( new WP_Customize_Color_Control ( $wp_customize, ‘chrome_theme’, array(
‘label’ => __(‘Chrome theme color’, ‘base-theme’),
‘description’ => __(‘Tab color in Chrome for Android’, ‘base-theme’),
‘section’ => ‘title_tagline’,
‘settings’ => ‘chrome_theme’,
)));

add_action( ‘customize_register’, ‘customizer_options’ );

then, I print the meta tag into my page with this function:

function chrome_theme_meta() {

echo ‘<meta name=”theme-color” content=”‘, get_theme_mod( ‘chrome_theme’, ” ), ‘”>’;

}

add_action(‘wp_head’, ‘chrome_theme_meta’);

It works as expected, except that it prints the meta tag also when no value is set, and I’d like to prevent this.

How can I check if the chrome_theme setting is set (so, not empty), and fire the chrome_theme_meta only if this is true?

Read more here:: How to check if a Customizer setting is set?

Leave a Reply

Your email address will not be published. Required fields are marked *