There’s not really any trick, you need to check all the values. You can save a little space though, by omitting the != ''
, which is redundant:
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' )
) {
However, it’s possible to save Customiser settings into a single array. So if you register your settings with names like:
$wp_customize->add_setting( 'myplugin[h2_color]' );
$wp_customize->add_setting( 'myplugin[h3_color]' );
$wp_customize->add_setting( 'myplugin[p_color]' );
$wp_customize->add_setting( 'myplugin[p_size]' );
Then you can retrieve the values in an array like:
get_theme_mod( 'myplugin' );
If you do this, then you could use array_filter()
to remove any empty values, and check if the resulting array is empty. If it isn’t, then at least one of the the settings has a value:
$styles = get_theme_mod( 'myplugin' );
if ( ! empty( array_filter( $styles ) ) ) {
echo '<style>';
if ( ! empty( $styles['h2_color'] ) {
printf( 'h2 { color: %s; }', $styles['h2_color'] );
}
// etc.
echo '</style>';
}