2011-09-29 44 views
1

建立一个WordPress选项面板。它所做的一件事就是允许用户选择自定义颜色。我使用默认的样式表,然后调用样式来定制输入。不过,我只是将这些插入头中,而不是让这些自定义值驻留在它们自己的样式表中(因为我需要通过PHP调用用户的选择)。这应该在它自己的样式表中,以及如何? (PHP样式表?)

举个例子,我有一些代码在我的标题是这样的:

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

在我的功能:

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" => ""),  

我怎么会有这种驻留在其自己的样式表,同时仍使用<?php echo get_option('to_custom_css'); ?>调用用户输入?

+0

...这是我一整天听到的最悲惨的消息。 – siouxfan45

+0

请参阅由daiscog回答什么是解决方法。 – Sparky

+0

@LucasWynne你可以将样式表的扩展名设置为.php。或者,如果您使用的是Apache,您可以设置任何特定的文件以供PHP处理,甚至可以使用mod_rewrite将some_file.css的所有请求传递给some_other_file.php。您只需要记住将PHP文件中的Content-type标头设置为“text/css”。 – megaflop

回答

6

您可以在PHP中创建样式表,但需要将Content-type标头设置为text/css。在你的HTML:

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

的用户,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... */ 
0

最终你应该写出来的CSS可以包括从主机页的文件。这样它可以被浏览器缓存。

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

浏览器可以缓存任何PHP文件。只需设置相关的HTTP标头。 – megaflop

3

首先,将它添加到您的.htaccess文件,以便它会解释的CSS文件中找到PHP:

AddType application/x-httpd-php .css 

然后链接到外部样式表。使链接包括动态,以确定用户的CSS值(可能是一个ID号)所需要的信息,这样的:

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

最后将PHP代码中动态地打印出的CSS,像这样的样式表:

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

使用$_GET['parametername']来检索允许您计算css数据的参数。