Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In another question I was talking about using theme options to modify CSS. This is a separate but related question.

When a user sets a link colour in my theme, here's how I'm including their choice:

<?php $options = get_option('mytheme_theme_options'); _e( 'a, a:link {color: #' ); echo $options['linkcolour']; _e( ';}' ); ?>

So, if a user specifies a link colour of #333333 in the theme options page, then my code above outputs a nice clean bit of CSS:

a, a:link {color: #333333;}

Great.

However, if they don't specify any link colour value, then I get this:

a, a:link {color: #;}

...which is both ugly and redundant -- it doesn't do anything.

I'm not a PHP person, so I don't really know the best way to fix this, but I imagine that it could be fixed with an if-else statement of some kind, where the code only gets output when the user sets a value. Is that right? If so, what code should I use?

Thanks.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

There's no need to be using _e() that's for text that's to be translated, you don't translate CSS, it only comes in one language...

With regard to your if/else'ing, try this..

<?php 
$options = get_option('mytheme_theme_options');
if( isset( $options['linkcolour'] ) && ( !empty( $options['linkcolour'] ) ) )   
    printf( "a, a:link {color: #%s;}", $options['linkcolour'] );
?>

Here's some PHP references for helping understand the PHP that was used above.

http://www.php.net/manual/en/control-structures.if.php
http://php.net/manual/en/function.isset.php
http://www.php.net/manual/en/function.empty.php
http://php.net/manual/en/function.printf.php

Hope that helps.

share|improve this answer
    
Awesome. :) t31os, good sir -- I thank you! Great answer. –  snthr Mar 9 '11 at 0:14
    
You're welcome.. :) –  t31os Mar 9 '11 at 0:15
    
As a related sub-question: how would I go about doing the same thing with output from other form elements -- checkboxes, radio buttons, drop-down select inputs, textareas, etc? Would the code be pretty much identical, or are there any changes? –  snthr Mar 9 '11 at 0:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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