1

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:

<?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.

Mindaugas
  • 1,173
  • 5
  • 15
  • 31
  • 1
    Couldn't you use sessions to pass information from file to file? I'm not sure exactly how wordpress handles sessions, but sessions are the only way to pass data from one file to another. – ja408 Jul 01 '15 at 23:11

2 Answers2

2

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).

Aibrean
  • 6,297
  • 1
  • 22
  • 38
  • Hi, thanks for suggestion, this is not really an option as I forgot to mention that there is two images that must be displayed based on few media queries. And it doesn't seem as a proper solution to put all of my queries in head section. Anyways, your idea in other case is awesome! :) – Mindaugas Jul 01 '15 at 23:13
  • You don't need the queries in the head. You add additional styles in the CSS. Use the inline style as basically "appending" to CSS. – Aibrean Jul 01 '15 at 23:14
1

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

rlb.usa
  • 14,942
  • 16
  • 80
  • 128