0

I'm using a dynamic CSS file with PHP extention.

wp_enqueue_style('css', get_template_directory_uri().'/inc/css.php');

The output:

<link rel='stylesheet' id='dynamic-css'  href='PATH/inc/css.php?ver=4.4.2' type='text/css' media='all' />

I'm also using a cache plugin (WP Super Cache).

My question is, is this CSS file cached as PHP or CSS?

2
  • Nobody knows?!! Mar 6, 2016 at 4:50
  • 1
    Keep in mind this is technically a security hole, as that css.php file needs to load WordPress, and will be active even if you switch themes or disable the plugin it's in. The same is true of files that handle forms or AJAX endpoints that are inside themes. Use the query vars and rewrite rules instead
    – Tom J Nowell
    Mar 14, 2016 at 0:53

2 Answers 2

3

A great deal of this will depend on your server configuration, but:

Will WP Super Cache cache the css.php output?

Extremely unlikely. If your file simply generates CSS from a few variables, then no.

If however it bootstraps and loads WordPress, then it 'might' cache some things, but I doubt it will work as you expect, or that it will do a full page cache. Here the word 'might' is a stretch at best

Will Other Caching Plugins Cache it?

Switching to another caching plugin will not change things

Will the Server cache it? Or the Browser?

That depends on the headers sent, and at this point we've moved beyond the scope of WordPress and this stack exchange. The answer is entirely dependent on your setup, and is impossible to answer without making this question useless to other people, or having intimate knowledge of your system

Is What I'm Doing Safe?

No.

If your file bootstraps WordPress, then it will work even when the plugin or theme is disabled, acting as a potential security hole. This is even more true of AJAX endpoints and form handlers. WordPress is a CMS, and all requests should be routed through it. An AJAX API is provided already, and there are rewrite rules and query variables that you can add and detect to output CSS and other things.

If however your css.php outputs PHP after doing a small amount of math etc, then you should eliminate it, and simply use a build process to generate a css file. You can use less or sass or some other system to generate the CSS, but it doesn't need distributing to your server

0

I don't know what you do in your css.php to handle your php cache, but i guess not much. If you want your css.php to be cached as CSS you can use this adapted snippet I found.

HOW TO CREATE A SIMPLE AND EFFICIENT PHP CACHE

Edit 2:

As stated in @TomJNowell 's answer this is not a safe approach:

If your file bootstraps WordPress, then it will work even when the plugin or theme is disabled, acting as a potential security hole. This is even more true of AJAX endpoints and form handlers. WordPress is a CMS, and all requests should be routed through it. An AJAX API is provided already, and there are rewrite rules and query variables that you can add and detect to output CSS and other things.

css.php:

$url = $_SERVER["SCRIPT_NAME"];
$break = Explode('/', $url);
$file = $break[count($break) - 1];
$cachefile = 'cached-'.substr_replace($file ,"",-4).'.css';
$cachetime = 18000;

// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
    echo "/* Cached copy, generated ".date('H:i', filemtime($cachefile))." */\n";
    include($cachefile);
    exit;
}
ob_start(); 

//Add your former css.php here

$cached = fopen($cachefile, 'w');
fwrite($cached, ob_get_contents());
fclose($cached);
ob_end_flush();

Edit:

Your Plugin caches all template output (posts, pages, archives, …) as HTML but no linked data, like JS or CSS.

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.