Using PHP in a Stylesheet (possibly a “.htaccess” problem?)

I’m sorry to be asking a question that’s (from what I’ve gathered) asked with relative frequency, but I think I’ve complicated the problem by leaving a project and then coming back to it after my skills have gotten rusty. So, in a very basic theme I created a while ago for a friend, I used PHP in the stylesheet to create some variables so that colors of certain elements can be randomized. The way I achieved this was to name the stylesheet style.php (as opposed to CSS), and included <?php header("Content-type: text/css; charset: UTF-8"); ?> at the top of the file.

This worked great at the time, but since I completed it, my friend has stopped using their website, and I decided I’d use the theme for my own personal site. I got a ManagedWP GoDaddy account and uploaded the theme, but now it is not working. Here is where I’m having trouble diagnosing the problem. The most obvious problem to me is that something is causing WordPress to no longer like using a PHP stylesheet, so one thing I’ve tried is to rename the stylesheet to style.css and then modify .htaccess to get it to be parsed as PHP. Modifying .htaccess is a new thing to me, so I may be doing something silly in there, but I’ve been using this article as a guide, and now the .htaccess in the theme’s directory (the same directory level as style.css) reads as such:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

<FilesMatch "^.*?style.*?$">
SetHandler php5-script
</FilesMatch>

However, I’m still not achieving the same result as before when WordPress seemed to be fine with using a PHP stylesheet, and I’m at a loss with what the right questions are to ask. I have a feeling a large part of this is retracing my steps, but I’m not sure which direction to go! Any help would be greatly appreciated!

3 Answers
3

Suggest you move your desired functionality to javascript; you are so badly attempting to overload the purpose of these constructs…even if you get it to work you are going to have a very fragile and difficult to maintain system on your hands.

Note that the idea of inlining style may work — but future Content Security Policy may kill that idea…inline CSS is a weak spot; see this OWASP explanation for more information.

Leave a Comment