I am using this code to add my options inside head section code works but my problem is that if no values are provided my function still echoes an empty <style type="text/css"><style>
into my head.
How can I remove style tags if no values are returned?
Thank You!!
//Hook into WP head and add our options
add_action('wp_head', 'dynamic_options');
function dynamic_options() {
$checkbox = get_option('sandbox_theme_social_options');
$checkbox['hide_sidebar'] ='';
$css = array( 'headercolor'=>'blank',
'wrapper_background_color'=>'blank' );
$css['headercolor'] = get_header_textcolor(); // get_header_textcolor();
$css['wrapper_background_color'] = get_option('wrapper_background_color');
foreach( $css as $itm => $value ) {
if($value !='blank') {
echo '<style type="text/css">';
if($css['wrapper_background_color']!=''){
echo "#wrapper{background-color:".$css['wrapper_background_color'].";
}"; break;
}//End if background color
if ($checkbox['hide_sidebar'] == 'on'){
echo '.sidebar_sec{display:none;}';
}//End if sidebar on
echo '</style>';
}//End If value Blank
}//End Foreach
}// End Dynamic options
You are using a code a bit dirty: foreach is not needed here, and I think you forgot to print the header color (see comment in following code).
After that, if you want to print nothing if you have no customiztion, create an out variable and it’s not blank print the styles:
How about checking whether the item is set beforehand?