i’m creating and theme and would like the user to be able to select their font family. Typically, i see that people use css.php files and pass user defined variables inside it.
I’m a bit concerned of that approach though, i would not like my css file actually be a php file, it also makes things a bit more difficult to maintain.
So, i would like to ask if maybe wordpress creators have thought of that and have added another way of specifying such custom values. Does anybody know if that can happen somehow without messing php with style.css itself ?
There’s two ways that this can be done.
Write CSS File
The first option would be to write your CSS file after the user chooses the custom font. I’m guessing that your theme will have an options panel of some sort. When the user submits the data, the new CSS file would be created using that data and any other data that your options panel creates.
The following is a function that I’ve used for something similar. It checks to see if the proposed contents of the new file are different from the existing file and saves the new data if necessary. You would need to modify this, especially as this does no data processing. This may or may not work for your situation, but it’s worth taking a peak
Internal Style Sheet
My other recommendation would be to use an internal style sheet for the dynamic data. You would load your static CSS content in an external style sheet, then dynamically, between style tags in the head of your header file, write the parts of the CSS that are dynamic. For instance, you could do something like in your functions.php file:
This could also be included directly in your header file, but using this method may make it play nicer with other plugins.
Of course, your normal stylesheet would be included in the header.php file with something like:
And the
wp_print_styles
action will only work if you have included thewp_head()
function somewhere in your header.php file (should be after you add your stylesheet).