Remove Internal Style Sheet if no Value Provided?

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

Related posts

2 comments

  1. 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:

    add_action('wp_head', 'dynamic_options');
    
    function dynamic_options() {    
    
      $checkbox = (array) get_option('sandbox_theme_social_options');
      if ( ! isset($checkbox['hide_sidebar']) ) $checkbox['hide_sidebar'] = 'off';
      $css = array(
        'headercolor' => get_header_textcolor() ? : 'blank',
        'wrapper_background_color' => get_option('wrapper_background_color') ? : 'blank'
      );
      $out = '';
    
      if( $css['wrapper_background_color'] != 'blank')
        $out .= '#wrapper { background-color:"' . $css['wrapper_background_color'] . '"; }';
    
      // I think you forgot following 2 lines in your code ;)
      if( $css['headercolor'] != 'blank')
        $out .= '#header { color: "' . $css['headercolor'] . '"; }';
    
      if ( $checkbox['hide_sidebar'] == 'on' )
        $out .= '.sidebar_sec { display:none; }';
    
      if ( $out != '') echo '<style type="text/css">' . $out . '</style>';
    
    }
    
  2. How about checking whether the item is set beforehand?

        foreach( $css as $itm => $value ) {
            if( isset( $itm ) ) {
                 echo '<style type="text/css">';
                 if($value !='blank') {
                      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
                 }//End If value Blank
                 echo '</style>';
            } //End If is set
        }//End Foreach
    

Comments are closed.