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

wordpress.stackexchange https://wordpress.stackexchange.com/questions/386951

  •  20-05-2021
  •  | 
  •  

Question

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-includes\option.php'); But this ends in => Call to undefined function wp_installing() Ok, let's includ this also include_once('wp-includes\load.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? Thank you :)

Was it helpful?

Solution

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 :))

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top