Passing variables between files in WordPress or PHP

In my inc/contact-form-processor.php files I set

$form_complete = FALSE;

in my template-contact.php file I have

Read More
<?php
/*
Template Name: Contact Page
*/
?>

<?php require_once 'inc/contact-form-processor.php'; ?>

<?php get_header();

    $SidebarPosition = sidebar_position()[0];
    $IndivSidebarPosition = sidebar_position()[1];
    $DefaultSidebarPosition = sidebar_position()[2];

?>

<div class="container">

    <div class="row">


     <?php if ( $SidebarPosition == 'left' ) {

                get_template_part( 'layouts/contact/left', 'sidebar' );

            }

            if ( $SidebarPosition == 'right' ) {

                get_template_part( 'layouts/contact/right', 'sidebar' );

            }

            if ( ( $IndivSidebarPosition == 'none' ) || ( $IndivSidebarPosition == 'default' and  $DefaultSidebarPosition == 'none' ) ) { 

                get_template_part( 'layouts/contact/no', 'sidebar' );

            }

    ?>

    <?php echo $IndivSidebarPosition = sidebar_position()[1]; ?>

    </div>

</div>

<?php get_footer(); ?>

I thought that by using require once and then referencing the contact-form-processor file $form_complete would be available in this file and the subsequent default

get_template_part( 'layouts/contact/right', 'sidebar' );

which displays a contact form based on the condition

            <div id="contact_form">
                <?php if($form_complete === FALSE) { ?>

                        <form>
                        .. Form ...
                        </form>

            <?php } ?>

            </div>

However, the form is not displayed and when I check for the $form_complete variable it is empty. How can I pass the variable through to both the files, I have read that I can use

You can use the WordPress locate_template function within PHP’s
include(). It’s done like this:

include(locate_template('your-template-name.php')); 

All of the variables available in your current script will be available in that
template file now too.

But I am not sure which file that code goes in and which file it is suppose to reference.

Related posts

1 comment

  1. your issues relate to scope. There is no magic trick to the snippet

     include(locate_template('your-template-name.php')); 
    

    Locate template just returns the filename (it looks in the theme to find the appropriate file, generally used for allowing overwriting by child themes). What is relevant to you is include will load the file into the same scope as the function/ line that called it.

    So lets look at this:

    $a= 'im outside scope';
    $b = 'im outside scope but get passed into the function so i can be used';
    
    function sample($var){
    
        $c= 'in scope';
        $d= $var;
    
        include 'template.php';
    }
    
    sample($b);    
    

    Template.php

     <?php
     echo $a; // ''
     echo $b; // ''
     echo $c; // 'in scope'
     echo $d; // 'im outside scope but get passed into the function so i can be used'
    

    So if you use get_template_part(), this is a function, only variables you have passed into the function (either in the arguments, by calling globals, class props) will be available in the template, get template part does not accept arguments you can use for this.

    So the solution is to replace your get_template_part() calls with include calls. This way you have variables in the same scope.

Comments are closed.