I'm in need of some CSS for a WordPress plugin which are dynamic and would like to know what is the best or the most common method. I'm currently using method two but have problems with IE9. So I thought there could be a better solution for dynamic CSS and came up with these:
wp_head
hookPro:
Contra:
admin_url('admin-ajax.php?action=my_css')
in a link tagPro:
wp_enqueue_style
Contra
wp-load.php
Pro
wp-load.php
)Contra
I don't like method one cause the style is not required on every page and method two doesn't work on IE9.
Should I go with the third one or are the any disadvantages with it?
Thanks in advance!
You are somewhat limited with PHP not having a built-in persistent cache. If you can guarantee that you will have memcached
, APC
, or even file write access, then you can use any of these methods to cache your CSS, and retrieve it using a key. You would not need to use wp-load.php
to do so, thus your performance would be improved over having to load all the plugins, etc.
That said, your dynamic CSS should work in IE9 assuming you set header('Content-type: text/css');
before you output the CSS, per the Microsoft article MIME-Handling Change: text/css.
All that said, you could try a hybrid approach of #1 and #2 - it sounds like #3 is out if you can'g guarantee that you have file write permissions. To implement, just detect the user-agent
of the requesting browser, and set up a single function to output your CSS. If it is not IE9, you can include the stylesheet using admin_url('admin-ajax.php?action=my_css')
and call your output from the hooked function, and if it is IE9 you can include the stylesheet in the header by calling the function from a hook to wp_head
. This way you are able to cache the CSS for most clients, and work around for IE - your cons about page size, etc are valid... but it's IE9.
As long as you are able to cache the CSS per client, then you are typically looking at only one extra request to wp-load.php
which shouldn't be too great of a performance hit.
You can check for IE9 using if (false!==strpos('MSIE 9;', $_SERVER['HTTP_USER_AGENT']))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With