开发者

Problem with get_option Wordpress function called in customizable stylesheet

开发者 https://www.devze.com 2023-03-26 07:06 出处:网络
I am developing Wordpress theme that I want to be flexible and I want the admins to be able to change the colors of the theme. That\'s why I decided to use st开发者_开发知识库yle sheet \"style.php\" t

I am developing Wordpress theme that I want to be flexible and I want the admins to be able to change the colors of the theme. That's why I decided to use st开发者_开发知识库yle sheet "style.php" that is generated during run time with the following code:

<?php 
    header("Content-type: text/css");
    $options = get_option( "option_group" );
?>

body {
    background-color: <?php echo $options["body-color"]; ?>
}

/* The rest of the css goes here......... */

and I included this file in the header section like normal style sheet. The problem is that I get "Call to undefined function get_option()" error in this file. I am wondering how can I make it work. In every other file where I call get_option() it works completely normal. I would be glad if you can give me any suggestion or work around.

Have a nice day :)


If the stylesheet is included as a <link> tag in header.php, like this...

<link href="http://YOURSERVER/wp-content/themes/YOURTHEME/style.php" media="all" type="text/css" rel="stylesheet">

then the style.php script doesn't have access to WordPress unless you load WordPress at the top of the script. Doing that would be tricky & resource intensive (you'd be loading WP twice for every page load.)

Probably a better, more efficient, way of doing this is to inject the custom styles directly in the <head> of the document like this:

<head>
...
<style>
body {
     background-color: #CCC;
}
</style>
</head>

To do this your theme can use the wp_head action hook...

add_action("wp_head", "my_print_custom_style");
function my_print_custom_style(){
    //look up the option
    //echo out the <style> tag and css
}

EDIT---- I made this more complicated than it needed to be. Since you're coding a theme rather than a plugin you can output the <style> tag directly in header.php rather than messing around with the wp_head action hook.

0

精彩评论

暂无评论...
验证码 换一张
取 消