Skip to main content
added 845 characters in body
Source Link
icc97
  • 820
  • 8
  • 18

This is a more detailed explanation of the first answer - i.e. "scope is you're problem"

My simple visualisation of get_template_part is like this:

function_get_template_partfunction get_template_part($slug,$name) {
  require("$slug-$name.php");
}

So if you have the following in sitewideSettings.php:

<?php
echo $nonce;
?>

Template with include:

<?php
$nonce = 'xxx';
include('sitewideSettings.php');
?>

Gets translated (by PHP) into:

<?php
$nonce = 'xxx';
echo $nonce;
?>

Template with get_template_part:

<?php
$nonce = 'xxx';
get_template_part('sitewideSettings');
?>

Gets translated into:

<?php
$nonce = 'xxx';
get_template_part(
  echo $nonce;
);
?>

So now anything in the template part is wrapped inside a function and any variables you have in your template part need a global on them.

Thus to get it to work you need a global in the sitewideSettings.php, which would get translated into:

<?php
$nonce = 'xxx';
get_template_part(
  global $nonce;
  echo $nonce;
);
?>

My visualisation of get_template_part is like this:

function_get_template_part($slug,$name) {
  require("$slug-$name.php");
}

So any variables you have in your template part need a global on them.

This is a more detailed explanation of the first answer - i.e. "scope is you're problem"

My simple visualisation of get_template_part is like this:

function get_template_part($slug) {
  require("$slug.php");
}

So if you have the following in sitewideSettings.php:

<?php
echo $nonce;
?>

Template with include:

<?php
$nonce = 'xxx';
include('sitewideSettings.php');
?>

Gets translated (by PHP) into:

<?php
$nonce = 'xxx';
echo $nonce;
?>

Template with get_template_part:

<?php
$nonce = 'xxx';
get_template_part('sitewideSettings');
?>

Gets translated into:

<?php
$nonce = 'xxx';
get_template_part(
  echo $nonce;
);
?>

So now anything in the template part is wrapped inside a function and any variables you have in your template part need a global on them.

Thus to get it to work you need a global in the sitewideSettings.php, which would get translated into:

<?php
$nonce = 'xxx';
get_template_part(
  global $nonce;
  echo $nonce;
);
?>
Source Link
icc97
  • 820
  • 8
  • 18

My visualisation of get_template_part is like this:

function_get_template_part($slug,$name) {
  require("$slug-$name.php");
}

So any variables you have in your template part need a global on them.