开发者

Should This Be In Its Own Stylesheet, and How? (PHP In Stylesheet?)

Building a WordPress options panel. One of the things it does is allow users to pick custom colors. I use a default stylesheet, then call styles for custom inputs. However, I simply insert this in the header instead of having these custom values reside in their own stylesheet (since I need to call the user's choice via PHP).

As an example, I have code in my header like this:

<style type="text/css">
p a { <?php echo get_option('to_custom_css'); ?> }
</style>

And in my functions:

array( "name" => "Custom CSS",  
    "desc" => "Want to add any custom CSS code? Put in here, and the rest is taken care of. This overrides any other stylesheets. eg: a.button{color:green}",  
    "id" => $shortname."_custom_css",  
    "type" => "text",  
    "std" => ""),    

How would I have this reside in its own stylesheet while still using 开发者_运维百科<?php echo get_option('to_custom_css'); ?> to call the users input?


You can create a stylesheet in PHP, but need to set the Content-type header to text/css. In your HTML:

<head>
  <link rel="stylesheet" type="text/css" href="user-styles.php" />
  <!-- ... -->

The in user-styles.php:

<?php

header('Content-type: text/css');

?>
/*  now put your css here : */

p a { 
    <?php echo get_option('to_custom_css'); ?> 
}

/* and so on... */


First, add this to your .htaccess file so that it will interpret php found in css files:

AddType application/x-httpd-php .css

Then, link to the external stylesheet. Make the link dynamically include the information needed to determine the user's css values (probably an id number), like this:

<link rel="stylesheet" href="http://www.yourserver.com/customstyle.css?id=<?php echo $userid; ?>" type="text/css" />

Finally, put php code in the stylesheet that prints out the css dynamically, like this:

<?php echo get_option('to_custom_css'); ?>

Use $_GET['parametername'] to retrieve the parameters allowing you to calculate the css data.


Ultimately you should be writing out the CSS to a file that can be included from the hosting page. That way it can be cached by the browser.

<link rel="stylesheet" type="text/css" href="user1024/style.css" />
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜