wordpresswordpress-themingget-headers

Files included in wordpress header.php are not accessible of page templates even after calling get_header()


I am developing a custom wordpress template. I have a few page templates for the layout.I call get_header() and get_footer() at the top and bottom of my page templates respectively.

Now the issues is. I have used two or three require_once() in the header.php file to include php class file. And in one of the included file , I have created an object for the included class files. But when I call those objects in my pages files (-- where I used get_header()--) it says undefined variable.

This is my wordpress header.php

// THIS IS MY header.php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

if(session_id() == '')
    session_start(); 

date_default_timezone_set('Asia/Dubai');

require_once('risk-profiler/configuration.php'); // Config file
require_once('risk-profiler/dal.php'); // One class files
require_once('risk-profiler/bl.php'); // another class file
require_once('risk-profiler/form_handler.php'); // Were Objects for the classes are created
?>

form_handler.php

if (!isset($data))
    $data = new DataAccessLayer(sql_server, sql_user, sql_password, sql_database);
if (!isset($bl))
    $bl = new businessLogic;

$data is my object for the database class and $bl is an object of another class.

Now this is where I call get_header() risk_profile_questionnaire.php and I included two form (risk-profile-select-profiling-country.php & another.php) in this file(form) is where I call that object and it is not accessible.

risk_profile_questionnaire.php is as

<div class="form-group">
            <label for="" class="col-md-4 control-label">Country : </label>
            <div class="col-sm-8">
                <select class="form-control input-lg" name="version_details">
                    <?php
                        $version_details = $data->get_version_list();
                        while ($row = mysqli_fetch_array($version_details)) {
                            echo"<option value='" . $row['country_code'] . "|" . $row['version'] . "'>" . $row['country_name'] . "</option>";
                        }
                    ?>
                </select>
            </div>
        </div>

Can anyone helpme on why my objects are not accessible at that point.


Solution

  • I can't test now, but my guess is that this is because of variables scope.

    If you are going to use a global variable inside a function in PHP, you need to declare it as global at the beginning of the function.

    Since you are including header.php (and the rest of files included from header.php) from the function "get_header", variables are limited by default to the scope of "get_header" function.

    Try to declare the global variables you need to use at the beginning of header.php file, for example:

    global $data;
    

    Variables scopes in PHP: http://php.net/manual/en/language.variables.scope.php