passing value between two php files

I have my wordpress page where I want to pass value between header.php and style.php(php version of style.css) files.

In header.php I have:

Read More
<?php $background = get_field('background')['url']; ?>

Which retrieves custom field value(uploaded img url).

In my style.php I have my css, but before that I have my <?php ?> tag where I can define some variables to use in stylesheet. Any ideas, how to retrieve this value from header.php and store in a variable on style.php?

Any help is very much appreciated, thanks.

Related posts

2 comments

  1. If I follow you correctly, you’re essentially wanting to have more control over the background image in CSS, but need to be able to allow the user control on the backend to define it.

    In that case I would do something simple in the header.php <head> like,

    <style>
    .background {
       background-image: url("<?php echo $background ?>");
    }
    </style>
    

    in the CSS file you can govern the rest of the control of the element.

    Here’s a real-world example of what I did today on a site.

    In my head:

       <style>
          .background {
              background-image:url(<?php the_field('background_image')?>);
           }
       </style>
    

    My HTML:

    <div class="background">
    Content goes here
    </div>
    

    My CSS:

        .background {
          background-size: cover;
          background-position: center center;
          height: 600px;
        }
        @media only screen and (max-width: 40em){
            .background {
              height: 400px;
            }
         }
    

    If you’re attempting to define a GLOBAL variable, then you put this above your variable:

    <?php global $background; ?>

    Then you can use $background on any other page (but if it’s being used in the header, then it’s already accessible on other pages since that’s a template part).

  2. Answering the question that you asked and not the problem that you have,

    You’d use $_REQUEST :

    <?php
    
    $_GET['foo'] = 'a';
    $_POST['bar'] = 'b';
    var_dump($_GET); // Element 'foo' is string(1) "a"
    var_dump($_POST); // Element 'bar' is string(1) "b"
    var_dump($_REQUEST); // Does not contain elements 'foo' or 'bar'
    
    ?>
    

    If you want to evaluate $_GET and $_POST variables by a single token
    without including $_COOKIE in the mix, use $_SERVER[‘REQUEST_METHOD’]
    to identify the method used and set up a switch block accordingly,
    e.g:

    <?php
    
    switch($_SERVER['REQUEST_METHOD'])
    {
    case 'GET': $the_request = &$_GET; break;
    case 'POST': $the_request = &$_POST; break;
    .
    . // Etc.
    .
    default:
    }
    ?>
    

    From : http://php.net/manual/en/reserved.variables.request.php

Comments are closed.