1

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
2

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>';

}
| improve this answer | |
1

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
| improve this answer | |
  • Well if I use your solution then it echoes two empty style tags:( – Dejo Dekic Aug 28 '13 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.