Saturday, 25 May 2013

How to read my settings inside CSS/PHP without including wp-load.php

How to read my settings inside CSS/PHP without including wp-load.php

I have following code inside my CSS/PHP file in order to read my settings:
<?php //Setup location of WordPress
//We need this in order to READ our settings and access the WordPress functions...
$absolute_path = __FILE__;
$path_to_file = explode( 'wp-content', $absolute_path );
$path_to_wp = $path_to_file[0];
//Access WordPress
require_once( $path_to_wp.'/wp-load.php' );
//Now continue with any WordPress stuff...
?>
I know it is a bad practice but i simply don't know how to read my settings without using code above. For example this is how I am retrieving my background color(works great):
<?php
 $wrapper_background_color = get_option('wrapper_background_color');
if($wrapper_background_color['wrapper_background_color']!=''){//Let's check if value is provided
?>
#wrapper{
background-color:<?php echo get_option('wrapper_background_color'); ?>
}
<?php } ?>
I am getting my stylesheet like so:
<link rel="stylesheet" type="text/css" media="all" href="<?php echo get_template_directory_uri() . ''/functions/dynamic_css.php'' ?>"/>
So now I am trying to read my settings without require_once( $path_to_wp.'/wp-load.php' ); (first code I posted), so i deleted that code and deleted call to my

stylesheet (<link rel="stylesheet" type="text/css" media="all" href="<?php echo get_template_directory_uri() . '/functions/dynamic_css.php'?>"/>)
Now i added code straight from Wordpress codex
function theme_styles() 
{

wp_register_style( 'custom-style',
get_template_directory_uri() . '/functions/dynamic_css.php',
array(),
'20120208',
'all' );
//enqueing:
wp_enqueue_style( 'custom-style' );
}
add_action('wp_enqueue_scripts', 'theme_styles');
So my dynamic stylesheet is added correctly (when viewing page source) but my background settings(background-color)is not applied:(
I know that i simply can put this inside header.php and it will work but i really want to read settings inside my dynamically generated file...
<style type="text/css">
<?php
$wrapper_background_color = get_option('wrapper_background_color');
if($wrapper_background_color['wrapper_background_color']!=''){//Let's check if value is provided
?>
#wrapper{
background-color:<?php echo get_option('wrapper_background_color'); ?>
}
<?php } ?>
Can someone please tell how can I read my my setting without using wp-load.php inside my file?

No comments:

Post a Comment