Accessing global variable in “CSS” (style.php)

后端 未结 2 1072
遇见更好的自我
遇见更好的自我 2021-02-09 20:18

I\'m doing a style.php CSS file so I can use some dynamic variables in the CSS within a Wordpress installation:



        
2条回答
  •  庸人自扰
    2021-02-09 20:43

    Here's an alternative solution to embedding php in a Wordpress .css stylesheet (the usefulness i'm not sure on) that doesn't require manipulation of the Wordpress core.

    Simply make the php embedded css file in your theme dir containing the usual code:


    embedded_style.php

    /* define document as css*/
    
    
    /* Example php variable declaration and function call */
    
    
    /* Begin php embedded css code below here */
    body {
        background: none;
        color: ;
        font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
        font-size: 10pt
    }
    

    Then import this dynamic file into your themes style.css so that you don't have to modify the Wordpress core.


    style.css

    /*  
    Theme Name: Mytheme
    Version: 1.0
    Description: This theme has php embedded css
    Author: You
    */
    @import url(embedded_style.php);
    /* Normal CSS below as required */
    

    my 2 cents

    The genesis of this snippet was an attempt to allow for varying directory names when importing css from a parent-theme to my child. I didn't like the idea of modifying core wordpress files however as most functions/hooks are not defined at the style.css runtime to interrupt the call it was necessary to find an alternate method. In the end I haven't used this for the same reasons I couldn't interrupt the file-call (too early to use wordpress convenient constants etc.), however hopefully it will come in useful for someone else.

提交回复
热议问题