简体   繁体   中英

enqueue dynamic css in wordpress doesn t work

I enqueing a php file to get dynamcis styles on my functions.php in the travelify-child folder

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');}

The file is enqueue correctly but it doesn t take the php code

This is a short exemple of my 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;
}

I tried to put the in the file

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

But it didn't make any difference.

Thank you for any help

Use AJAX to solve this:

General

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');

dynamic-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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM