1

I thought about adding it as inline CSS with code from the functions.php (not really sure how to do it).

Something like this:

function addcss() {
   background: $this_is_the_css_value;
}

I would like to know that. But if there's is a better option (without adding inline CSS). I would like to know it too.

2
2

One way of doing this would be to create a "CSS" file from PHP. In other words, create a file, call it something like style.css.php, and at the beginning of the file put:

<?php header("Content-type: text/css"); 

Then, link that file in the head of your theme file. Because the style.css.php file is a PHP file, anything you can do in a normal PHP file can be done in this file. As such, you can pull theme option values from the database and use them. For example:

#header{
    background: <?php echo get_option('my-header-background-color'); ?>
}

Of course, you need to get the options into the database first, but since your question didn't address that, I won't go into it here.

2
  • 1
    Thanks! I did the database part with this tutorial: net.tutsplus.com/tutorials/wordpress/…
    – janoChen
    May 9 '11 at 4:10
  • 4
    The best way is to write the file, after saving in the admin option panel to a fixed .css file and then enqueue that file. You don't want to re-run php for EACH page load ... (for the same css output). See other answers on the same question.
    – edelwater
    May 9 '11 at 16:40
2

Generating CSS from PHP means it has to run that code for each pageload, if it is possible generate a static CSS file and then enqueue it.

So... See:

How do I add CSS options to my plugin without using inline styles? or

Best way to inject css into admin_head in plugins? or

How to add CSS style sheet dynamically in wordpress

2
  • Also this one, wordpress.stackexchange.com/questions/7048/…
    – Wyck
    May 9 '11 at 16:53
  • +1 for the great suggestion! My preferred option would actually be to place the custom CSS in the head, which would avoid writing a file and doing the css.php business. As the OP did not want inline CSS, I thought the css.php trick would be ok, but I also really like your solution. The only downside to it, in my opinion, is potential issues with file permissions that cause difficulties writing the file.
    – tollmanz
    May 10 '11 at 3:13

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.