Is there a simple method to passing data from one file to another? I’m new to PHP and can’t find a simple answer to my problem online. Apologies if this has been answered a million times & thanks in advance.

My Template 

<?php

// Declare a variable with the name of the file I want to pull in 
$critical_css="style";

// Header
get_header();

?>
Header 

// Get php file with inline style written inside
<?php get_template_part('/path/to/' . $critical_css); ?> 

2 s
2

The WordPress template functions don’t support passing variables (to my knowledge) by default, so you need to write your own function. For example like this,

// functions.php
function my_template_include_with_variables( string $path="", $data = null ) {
  $file = get_template_directory() . "https://wordpress.stackexchange.com/" . $path . '.php';
  if ( $path && file_exists( $file ) ) {
    // $data variable is now available in the other file
    include $file;
  }  
}

// in a template file
$critical_css="style";
// include header.php and have $critical_css available in the template file
my_template_include_with_variables('header', $critical_css);

// in header.php
var_dump($data); // should contain whatever $critical_css had

UPDATE 18.8.2020

Since WordPress 5.5 you are able to pass additional arguments with the native template functions (e.g. get_header(), get_template_part(), etc.) to the template files in an array.

From dev docs,

get_template_part( string $slug, string $name = null, array $args =
array() )

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *