In my theme I use theme options to set colors to categories (heading colors, dividers colors etc etc) The way I do it is defining variables which holds a color hexa like so:

$category1_color = get_option('category1_color');
$category2_color = get_option('category2_color');

etc etc..

and inside my admin I set the color (e.g - #ffccoo or yellow) in a "theme options" I already built where my client can easily change global colors to each category element in a second.

like so:

if (is_category("news")) { echo ' style="border-bottom-color:' . $category1_color . ';"'; } ?>

now - my client has a custom top menu which has items (mostly categories)

I want him to also be able to change the color of the texts in the menu. now, I know it is possible to add class to each menu item (this is fairly easy) but I dont want to create classes in the CSS file because it is not dynamic what I want it to grab the variable which was already changed by the client eg - $category1_color (lets say it is set to purple right now) and assign it dynamically to a class called "category1-item" which is used as a class on the menu item so if later the client is changing the color in my theme options it dynamically changes the color of the menu item too without going to the CSS file and edit it.

I wwas thinking about setting a class in the functions.php file and simply stored the variable I already created (which holds a color already) in this class like so:

category1-item = $category1_color

I just don't know how to add classes to the functions.php file and store a variable into each class.

any idea?

  • 1
    Why not simply parse the stylesheet with php? Commented Mar 23, 2013 at 12:58
  • I have never done that. Do u have any example? Reference? Also I don't want to parse the whole stylesheet. Just this part that I need. Is it possible?
    – gil hamer
    Commented Mar 23, 2013 at 13:53

1 Answer 1


First off, the origin of the data (WordPress theme options) aside, this question is pretty much about generic PHP and CSS.

That being said, here's how:

PHP Stylesheet

Add a new file to your theme, call it "dynamic_styles.php" or the like. Note that while it will contain mostly CSS, you give it a .php file extension.
This tells the server to parse the file with the PHP interpreter before serving it to the client.

In the first lines of the new file you set the content-type to "css":

    header("Content-type: text/css; charset: UTF-8");

Thereafter grab the values like you normally would (going with your above example):

    header("Content-type: text/css; charset: UTF-8");

    $category1_color = get_option('category1_color');
    $category2_color = get_option('category2_color');

Now use these in your later CSS as you would with HTML markup:

 div.cat-1 {
    border-bottom-color: <?php echo $category1_color; ?>;


While the above would work flawlessly, if the variables had static values, you have to go an extra step to use WP functions in your stylesheet.

How to go about that has been explained in this excellent answer.

  • Thanks for the answer. I just don't understand 2 things: 1) How do I reference the new styleshhet to the theme 2) where do I put this code: div.cat-1 { border-bottom-color: <?php echo $category1_color; ?>; } as I obviously cant put it in my regular css stylesheet Thanks!
    – gil hamer
    Commented Mar 23, 2013 at 17:40

Your Answer

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

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