Don't print customizer styles when no setting has been used
I'm adding some options to a theme through my plugin, I want to change this theme styles and I'm almost there... Now I need to know how I can hide the style
tag when no settings are changed (all default not used),
function myplugin_customize_css(){
?
style
h2 {color:?php echo get_theme_mod('myplugin_h2_color'); ?;}
h3 {color:?php echo get_theme_mod('myplugin_h3_color'); ?;}
p {
color:?php echo get_theme_mod('myplugin_p_color'); ?;
font-size:?php echo get_theme_mod('myplugin_p_size'); ?;
}
// more more of these!
/style
?php
}
add_action( 'wp_head', 'myplugin_customize_css');
I can do if()
statements here but it feels too much, what other ways I can do this... not like this:
function myplugin_customize_css(){
if (get_theme_mod('myplugin_h2_color') != ''
|| get_theme_mod('myplugin_h3_color') !=''
|| get_theme_mod('myplugin_p_color') !=''
|| get_theme_mod('myplugin_p_size') !='' ) {
?
style
// if get_theme_mod('myplugin_h2_color') != ''
h2 {color:?php echo get_theme_mod('myplugin_h2_color'); ?;}
// if get_theme_mod('myplugin_h3_color') != ''
h3 {color:?php echo get_theme_mod('myplugin_h3_color'); ?;}
// if get_theme_mod('myplugin_p_color') !=''
// get_theme_mod('myplugin_p_size') !=''
p {
// if get_theme_mod('myplugin_p_color') !=''
color:?php echo get_theme_mod('myplugin_p_color'); ?;
// if get_theme_mod('myplugin_p_size') !=''
font-size:?php echo get_theme_mod('myplugin_p_size'); ?;
}
// more more of these!
/style
?php
}
}
add_action( 'wp_head', 'myplugin_customize_css');
Topic theme-customizer get-theme-mod loop plugin-development Wordpress
Category Web