将动态css放入wordpress中是不起作用的


enqueue dynamic css in wordpress doesn t work

我在travelfy子文件夹中查询一个php文件,以获取我的functions.php上的dynamcis样式

add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'style_perso', get_stylesheet_directory_uri() . '/styleperso.php');}

该文件已正确入队,但没有使用php代码

这是我的styleperso.php的一个简短示例

<?php
header("Content-type: text/css; charset: UTF-8");
 $newbgcolor ='red';
$newbgimage = 'none';
?>
body.custom-background {
background-color: <?php $newbgcolor ?>;
background-image: <?php $newbgimage ?>;
background-repeat: no-repeat;
background-position: top right;
background-attachment: fixed;
background-size: cover;
}

我试着把放在文件中

require '/../../../wp-load.php';

但这并没有什么区别。

感谢您的帮助

使用AJAX来解决这个问题:

常规

function theme_enqueue_styles() {
    wp_enqueue_style('dynamic-css', admin_url('admin-ajax.php') . '?action=dynamic_css', null, null);
}
add_action('wp_enqueue_scripts', 'theme_enqueue_styles');
function dynamic_css_action() {
    include ( 'dynamic-css.php' );
    exit;
}
add_action('wp_ajax_dynamic_css', 'dynamic_css_action');
add_action('wp_ajax_nopriv_dynamic_css', 'dynamic_css_action');

动态css.php

<?php
    header('Pragma: public');
    header('Cache-Control: max-age=86400');
    header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 86400) . ' GMT');
    header('Content-Type: text/css; charset=UTF-8');
    $newbgcolor ='red';
    $newbgimage = 'none';
?>
body.custom-background {
    background-color: <?php echo $newbgcolor ?>;
    background-image: <?php echo $newbgimage ?>;
    background-repeat: no-repeat;
    background-position: top right;
    background-attachment: fixed;
    background-size: cover;
}