How to import style.php into style.css?

I have a brilliant idea to import a style.php into my child-theme's style.css but can't get it to work. Here's what I have at this time...

/*
Theme Name: Lilac
Template: bcstarter
*/

@import url("../bcstarter/style.css");
@import url("style.php");

and in the style.php I have...

body {
background: #D9D3AE url(images/lilac-bg.png);
}

The style.php file is in the theme directory along with the style.css, so I think I have the url correct, but I also tried every url I could think of. What am I doing wrong?

Note, my reason for this is to avoid loading customizer generated styles in the <head> section. It would instead be cached along with the style.css and thus lighten the load on web browsers. Of course, the property values would be replaced with php code once I get it working. Please tell me if I'm going about this the wrong way... I'm still learning. :slight_smile:

Thanks

  • Tyler Postle

    Hey Tedster,

    Hope you're doing well today and thanks for contacting us!

    Since CSS files are just CSS and browsers won't process PHP in them you wouldn't be able to import php code in. You can do it the other way around where you import css files into php files.

    In this case, you can just copy and paste that style into your css file :slight_smile:

    Or if you are wanting to change it, then just place the changed version into your css file.

    Of course, the property values would be replaced with php code once I get it working.

    I'm a little confused on this part. You want to give the styles php values? Perhaps I'm misunderstanding here!

    Look forward to hearing back Tedster!

    Have a great rest of your day.

    Cheers,
    Tyler

  • Tedster

    Browsing through my old topics and noticed this one never resolved. Here's what I ended up doing...

    Create a file my-style.php, give it a content type of text/css, and require /wp-load.php.

    my-style.php

    <?php
      header('Content-type: text/css');
      require_once $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';
    ?>

    Enqueue it in your functions.php file as normal

    functions.php

    <?php
    wp_enqueue_style('my-style.php', get_template_directory_uri() . '/style.php');
    ?>

    Now you can get_theme_mod('something_awesome') directly in your stylesheet. Pretty cool, eh?

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.