0

I want to register a new css document that I'm storing inside a PHP file (so that I can create variable css rules). I tried doing this:

function tps_admin_scripts() {
$plugin_url = plugin_dir_url( __FILE__ );
wp_register_script('admin-variable-style', $plugin_url.'css/style-variable.php');
wp_enqueue_script('admin-variable-style');
}
add_action('admin_enqueue_scripts', 'tps_admin_scripts');

And I've also tried wp_enqueue_style with no luck.

Additionally, I tried

require_once (plugin_dir_path(__FILE__).'css/style-variable.php');

But that simply rendered the raw style/html onto the page.

Inside the .php file, I have something like this:

<?php header("Content-type: text/css"); ?>
<?php
//Calendar Color Pickers
$tpsSettings = get_option( 'tps_rental_settings' );
?>

.colorAvailable {
    background-color: <?php echo $tpsSettings['tps_calendar_color_available']; ?>
}

Is this possible to do with WP? If not, any other solutions to including a php file to run as an additional css file?

2 Answers 2

1

You could also look to create a special CSS file that you setup an htaccess rewrite rule for. For example

wp-content/yourtheme/custom.css

And then add a rule in your htaccess:

RewriteEngine On
RewriteRule ^/wp-content/yourtheme/custom.css$ /wp-content/yourtheme/styles.php [L]

You can then call wp-load inside this styles.php file, do any sort of logic required, before outputting your custom css rules:

<?php header("Content-type: text/css; charset: UTF-8"); ?>
p {
   color: black;
}
1
  • Thanks, that's perhaps a cleaner solution from a themeing perspective!
    – Eckstein
    Commented Oct 28, 2017 at 20:02
0

That answer was to put this in my php file:

require_once('../../../../wp-load.php');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.