dynamic css file for admin / backend and get_option results in Uncaught Error: Call to undefined function get_option()

I try to style the admin area with a dynamic css. I need to configure some elements regarding the frontend user defines. He can choose a color and this color should reflected in some elements in the admin area (e.g. buttons in admin area).
My approach: Setting up a evacolor.css, but let apache interpret this as a php file. This works so fare:

.htaccess in the same dir, as my color.css:

<FilesMatch ".css$">
    SetHandler application/x-httpd-php
    Header set Content-type "text/css"
</FilesMatch>

My evacolor.css looks like:

<?php
 $absolute_path = explode('wp-content', $_SERVER['SCRIPT_FILENAME']);
 $wp_load = $absolute_path[0] . 'wp-load.php';
  header("Content-type: text/css; charset: UTF-8");
  header('Cache-control: must-revalidate');

    $test = "#ff00ff";
?>
 
body {
  background: <?php echo $test;?>;
}
.... and so on

This works fine. Now I need to pull my color out from the wp_options table and all the problems starts.

  1. I tried $color_items = get_option( 'cornerstone_color_items' );
    => Uncaught Error: Call to undefined function get_option()
    I tried to include include_once('wp-includesoption.php'); But this ends in
    => Call to undefined function wp_installing()
    Ok, let’s includ this also include_once('wp-includesload.php');
    => Call to undefined function apply_filters()

Hence.
Next try:

global $wpdb;    
$result = $wpdb->get_results( "SELECT * FROM wpeva_options WHERE option_name = 'cornerstone_color_items'");
print_r($result)

=> Uncaught Error: Call to a member function get_results()

I include require_once($wp_load);
=> Your PHP installation appears to be missing the MySQL extension which is required by WordPress.

I give up. No more ideas.

Does anyone has an idea, how to use a dynamic css (for admin, resp. backend) and use inside this css a color value, which is stored in wp_option table?
Even my attempt to define a global variable has had no success: $GLOBALS['color'] = '#ff00ff'; and also define('CICOLOR', '#ff00ff'); are empty in my dynamic css;

Any ideas?
Any help?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Thank you @Jacob for pointing me in the right direction.
This was an overkill!

=> in function php I call my values, set this in a global css variable.
Pick this variable in my admin css.

Again: Thank you! Lifesaver :))


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x