Dynamic CSS using WP AJAX
-
I’ve been trying to generate dynamic CSS in a plugin I’m working on without including wp-load.php.
I found a good solution here:
https://www.ads-software.com/support/topic/best-way-to-create-a-css-file-dynamically?replies=22Hube2’s method at the bottom seems to work very well to generate CSS using PHP variables; I’ve been able to echo PHP variables to use as CSS and my plugin will correctly display this CSS.
For example, this code will successfully color the background:
style.css.php:
<?php header('Content-type: text/css'); $color = "#F00"; ?> body{ background-color: <?php echo($color); ?>; }
Output:
body{ background-color: #F00; }
My issue is that according to Hube2’s post, I should have access to WordPress and all of its functions. This doesn’t seem to be the case though. First, my global variable for accessing my plugin’s options didn’t seem to be working. I tried calling the get_option() function to load the options again instead of using the global var and this caused nothing to output:
style.css.php:
<?php header('Content-type: text/css'); $color = get_option('color'); ?> body{ background-color: <?php echo($color); ?>; }
output: (none)
When I was including wp-load.php to get access to WordPress in order to use the plugin’s options, this code would’ve worked fine. I tried using other random WordPress functions and each time I tried to use one it would result in no output.
So can anybody explain why or how this isn’t working, or how I can access WordPress functions or my global var from this css/php file? It seems like this PHP is being interpreted before WordPress is loaded…
- The topic ‘Dynamic CSS using WP AJAX’ is closed to new replies.