I have the following, to run a simple form in the very top of my wordpress theme header.php file:
<?php
//deal with theme embeded forms
if ($_POST['enquery_page_loop']) {
$call_time = $_POST['call_time'];
$f_name = $_POST['r_name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$admin_email = get_option('admin_email');
$message = "A callback request has been recieved.rnrn";
$message .= "Name: ".$f_name."rn";
$message .= "Time to Call: ".$call_time."rn";
$message .= "Email: ".$email."rn";
$message .= "Phone: ".$phone."rn";
mail ($admin_email, "A Callback request was recieved via the website", $message, 'FROM:'.$admin_email);
$loop_mail_done = '1';
echo $loop_mail_done;
}
?>
After I call <?php get_header(); ?>
in the page.php file, the $loop_mail_done;
variable is completely inaccessible, as if it was never defined. Yet when I echo it from the bottom of the header.php file, it is defined correctly.
I’m at a loss to understand how I am losing this variable.
I’ve checked / tried:
- declaring
global $loop_mail_done;
in the page.php - correct scope (as far as I can see)
- checked for
unset
or similar actions on the variable
As has been said using
in both of the files will sort out the problem.
The reason for the issue is that both header.php and the page.php/index.php etc are invoked by functions upon page rendering. Whilst it looks to you like you’ve written something in the same scope, they are actually two variables in two seperate functions.
An alternative is to use $GLOBALS to store the variable, but declaring it global does as well.
Set
in both page.php and header.php before using this variable.