WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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?


share|improve this question
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..

$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.


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


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.