0

I'm developing a theme where I'm using theme options to allow users to set various things -- text size, background colour, link colour, etc.

The basic code I'm using for the options is in header.php and looks like this:

<?php 
$options = get_option('mytheme_theme_options');

if( isset( $options['backgroundcolour'] ) && ( !empty( $options['backgroundcolour'] ) ) )   
printf( "body {background: #%s;}", $options['backgroundcolour'] );

if( isset( $options['linkcolour'] ) && ( !empty( $options['linkcolour'] ) ) )   
printf( "a, a:link {color: #%s;}", $options['linkcolour'] );

?>

One of the options I want to set is a border radius for some chunks of content, which requires multiple declarations of the same value to work in all modern browsers:

.post {-webkit-border-radius: 8px; -moz-border-radius: 8px; border-radius: 8px;}

So, how do I get the PHP above to declare the same value three times in my CSS?

I should add that I'm not normally a PHP person; feeling a bit out of my depth here. :)

Thanks.

1 Answer 1

1

Use standard echo or printf with additional parameters. e.g.

if(isset($option['borderradius']) && (!empty($option['borderradius']))) {   
    $borderradius = '.post { ';
    $borderradius .= 'border-radius: ' . $option['borderradius'] . 'px; ';
    $borderradius .= '-webkit-border-radius: ' . $option['borderradius'] . 'px; ';
    $borderradius .= '-moz-border-radius: ' . $option['borderradius'] . 'px; ';
    $borderradius .= '}';

    echo $borderradius;
}

if(isset($option['borderradius']) && (!empty($option['borderradius']))) { 
    printf('.post { border-radius: %spx; -webkit-border-radius: %spx; -moz-border-radius: %spx; }', $option['borderradius'], $option['borderradius'], $option['borderradius']);
}
3
  • Hi Roman... Thanks for the reply, I'll try it out tomorrow and see if it works. Do you know, though, if there is any simpler/smaller way of getting the same result? I thought that it might be do-able with less code.
    – snthr
    May 3, 2011 at 20:55
  • The second part only requires 3 lines of code. It's really basic php, so may answer here is no. Maybe you want to checkout the PHP tutorial of Quakenet if need more help on PHP.
    – Roman
    May 4, 2011 at 9:45
  • Hi Roman... Thanks, I hadn't realised that you'd given me two different ways to do the same thing, I thought it was all one big lump of code. :) I went with the second way, worked great. Thanks!
    – snthr
    May 4, 2011 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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