2

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!

  • you might want to just reconsider your logic and place the php-set-css values within a <style> tag in your site using add_action('wp_footer',function(){ ?><style> ... </style> <?php }); or wtv. – David Sword Feb 9 '18 at 17:28
  • 1
    "something is causing WordPress to no longer like using a PHP stylesheet" - what do you mean by this exactly? What actually happens? Is the CSS served but with an incorrect mime-type? Is the file not served at all? File served, but the PHP is not parsed? "I'm still not achieving the same result as before" - and what exactly do you mean by this? How does the result differ from your initial approach? – MrWhite Feb 10 '18 at 22:08
2

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.

2

At this point in wordpress life it is just something you should not do (and should not have done in the first place). Experience shows that this kind of CSS file is very fragile* and easily breaks when wordpress related server setup is not the vanilla one, something that is probably true with all managed services.

Just add whatever you want as an inline style in your theme using the wp_head hook. (no, they are not a weak spot and it will not get killed in any future, unless someone will want to break all sites on the internet)

*this kind of files were known to have security problems, and break when directory structure changed

1

(You appear to have 2 WordPress code blocks for some reason...?! I assume that's some kind of copy/paste error in your question?)

and now the .htaccess in the theme's directory (the same directory level as style.css) reads as

Creating multiple .htaccess files in different directories is likely to cause more problems. (Is this why you have duplicated the WP front-controller?) You should add the relevant directives to the main .htaccess file in the document root, but be careful of the ordering.

something is causing WordPress to no longer like using a PHP stylesheet
:
WordPress seemed to be fine with using a PHP stylesheet

WordPress itself shouldn't have anything to do with the CSS/PHP file. However, your "ManagedWP" hosting may have additional security that is preventing these "rogue" (or rather, non-WordPress) PHP files being parsed?

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

Parsing all files that simply contain the word "style" as PHP could potentially cause problems. However, using SetHandler can be very server specific, so this may not work anyway.

An alternative is to keep the file named style.php (ie. with a .php file extension), but reference this as style.css in your HTML. Then, using mod_rewrite in .htaccess internally rewrite a request for style.css to style.php before the WordPress front-controller.

For example:

RewriteRule ^url-path/to/style\.css$ /filesystem-path/to/style.php [L]

(Stress... this must go before the WordPress front-controller.)

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.