【问题标题】:Using WordPress' parse_request function for dynamic CSS将 WordPress 的 parse_request 函数用于动态 CSS
【发布时间】:2011-02-03 09:32:24
【问题描述】:

按照http://josephscott.org/archives/2010/03/database-powered-css-in-wordpress-themes/ 的教程,我正在尝试使用 wordpress 的 parse_request 函数来添加一些 php 驱动的 CSS...主要是在我的主题选项面板中设置的样式选项。我知道我的代码看起来与作者的有点不同,但我已经按照他的方式尝试过了。我可以添加

function kia_wp_head() {
  wp_enqueue_style('dynamic', get_bloginfo('stylesheet_directory') . '/admin/ . '?my-custom-content=css');
}
add_action('wp_print_styles', 'kia_wp_head');
//this shows up properly enqueued but when i click on it in source it just brings up a directory listing for the admin folder


function my_custom_wp_request( $wp ) {
    if( isset($_GET['my-custom-content']) && $_GET['my-custom-content'] == 'css'  ) {

        # get theme options
        header( 'Content-Type: text/css' ); ?>

body {
    background-color: <?php echo 'red'; ?>
}

<?php
        exit;
    }
}
add_action( 'parse_request', 'my_custom_wp_request' );

但由于背景永远不会变红,我要么没有正确实现这一点,要么本教程缺少关键步骤。我还尝试了将动态 css 放入其自己的 custom-css.php 文件的另一种方法,这是我的最终目标,但我只是想看看我是否可以正确地与解析请求函数交互:

function my_custom_wp_request( $wp ) {
    if (
        isset($_GET['my-custom-content'])
        && $_GET['my-custom-content'] == 'css'
    ) {
        # get theme options
        header( 'Content-Type: text/css' );
        require dirname( __FILE__ ) . '/custom-css.php';
        exit;
    }
}
add_action( 'parse_request', 'my_custom_wp_request' );

这里我不确定 dirname(FILE) 的确切含义,但我也尝试过使用硬编码路径,但也没有用。

那么我如何获得 parse_request 以查看我的 php 驱动样式表?

/* 编辑解决方案 */

基本上这对 wp_enqueue_style 不起作用

wp_enqueue_style('dynamic', get_bloginfo('stylesheet_directory') . '/admin/ . '?my-custom-content=css');

但是通过将样式标签直接插入头部,可以按照 josephscott.org 中的描述工作

. '/admin?my-custom-content=css">

【问题讨论】:

    标签: css wordpress


    【解决方案1】:

    我发现它不适用于 wp_enqueue_script。通过手动将脚本标签添加到标题中,它确实可以按照教程中的说明工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-22
      • 1970-01-01
      • 1970-01-01
      • 2012-06-09
      • 2011-04-02
      • 1970-01-01
      • 2016-04-19
      • 2015-12-26
      相关资源
      最近更新 更多