Support » Plugins » Elementary PHP problem

  • Resolved Ian Stewart


    Theme Wrangler

    I’m having what is likely an elementary PHP problem …

    Order of what’s happening:

    in functions.php …

    global $var, $varcolor;
    function get_var() {
    global $var;
    $var = 'pink';
    This is just an example, I'm actually looking for a URL
    in a custom field. Which is why I have this running at get_header,
    so I can global $post and find the key and value.
    I also need to know if I'm looking at the home page or a post page
    with conditional tags.
    $varcolor = $var;

    functions.php runs, does it’s business.

    We get the header loading, $varcolor = ‘pink’ and all is right with the world. The header finishes loading, style.css loads and imports style.php like so …

    @import "style.php";

    Style.php does it’s thing but! it won’t find $varcolor. When I try and use $varcolor like

    body {background:$varcolor;}

    nothing happens. Everything thing else in style.php (including other dynamic stuff I’m doing there) works. It should set the background as pink, right?

    It’s probably some elementary PHP thing that I’m missing. Any clue?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Rick Beckman


    Ian, @importing a file is different from include()ing one. The style.php file you’re importing will be compiled completely on its own — unaware of any processing done in functions.php, header.php, or other theme files.

    That’s why your variable isn’t getting passed in.

    You’ll need to do get the variable defined in either style.php itself or in files include()ed therein.

    …unless I’m completely misunderstanding what you’re trying to do.




    You need to globalize your variables in the style.php file.


    Ian Stewart


    Theme Wrangler

    I tried globalizing the variables in style.php … to no avail. I think KingdomGeek is right, I’ll have to try a different approach.

    Thanks, guys!

    Hi, Ian — I thought I’d post the results of my efforts in hope it might help someone.

    I have found two ways to accomplish what you’re doing.

    Method 1: You can simply place the CSS via a hook. The only real downside is that the theme user would have to edit functions.php instead of style.css.

    That solution looks like this:

    function mytheme_custom_css() {
      print "<style type=\"text/css\">#wrapper { background: url($customurl) !important; }</script>\n";
    add_action('wp_header', 'mytheme_custom_css');

    Method 2: Have your theme create a cookie-based session. Then you can still do everything in style.php! We can use the $_SESSION “superglobal” to get the value.

    That solution is here:

    --- functions.php    (revision 645)
    +++ functions.php    (working copy)
    @@ -36,9 +36,17 @@
    +function mytheme_start_session() {
    +  session_name("wp_mytheme");
    +  session_start();
    +  session_register('customurl');
    +add_action('init', 'mytheme_start_session');

    Then in style.php:

    body { background: url({$_SESSION['customurl']}) !important;}

    -Gregory Grubbs

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Elementary PHP problem’ is closed to new replies.