How do I declare a global variable in PHP I can use across templates?

How do I declare a global variable in PHP I can use across templates?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Sharing Data Between header.php and Page Templates in WordPress

In the standard WordPress theme architecture, the loading sequence is rigid: header.php loads first, followed by the specific content template (e.g., index.php, single.php, or a custom file). If you need to pass data from that initial load step down to every subsequent view within the same request lifecycle, you are dealing with scope isolation. You cannot simply declare a global variable in the PHP block inside header.php and expect it to be accessible later in footer.php or your custom templates without understanding how WordPress handles variable scoping and the potential side effects of polluting the global namespace.

The most robust and idiomatic solution is to leverage the wp_localize_script() function or, more commonly for PHP variables, the global keyword combined with careful naming conventions. However, relying on bare globals in modern development is often discouraged because they create tight coupling between files that are difficult to debug. A better approach involves utilizing WordPress's built-in data storage mechanisms designed specifically for this transfer of state between template parts.

If you absolutely require a simple variable accessible across all templates without using the WordPress Loop or specific hooks, you can declare it globally within header.php. Place your code immediately after the PHP opening tag in header.php:

<?php
// Declare global variable in header.php
$global_theme_var = 'Data from Header';
?>

While this works technically because the execution context remains within the same request, it is fragile. If another plugin or a child theme inadvertently declares $global_theme_var without checking if it exists, it will overwrite your value. Furthermore, any function called inside header.php that expects to use its own local variable with the same name could cause unexpected overwrites. This lack of encapsulation makes refactoring difficult and can lead to subtle bugs where a variable appears empty in one template but populated in another due to timing issues or plugin conflicts.

A superior architectural pattern involves passing data through the WordPress Data Store or using a dedicated action hook like wp_footer with a filter, though for simple variable sharing within your own theme files, defining a class property is often cleaner than using globals. You can encapsulate shared data in a singleton class defined once and referenced everywhere:

// In header.php
class ThemeData {
    public static $shared_value = 'Hello from Header';
}
?>

Then in your other templates:

<?php echo ThemeData::$shared_value; ?>

This approach maintains the global accessibility you need while providing a namespaced container that prevents collision with other code. It also allows you to add getters and setters or methods to manipulate the data without exposing it directly to the global scope's pollution risks.

When implementing these solutions, remember that these variables persist only for the duration of the current request. If you attempt to access these values in a subsequent page load or an AJAX call initiated from another domain, they will not be available unless you explicitly serialize and output them (e.g., using wp_localize_script for JavaScript contexts). Additionally, if you are building a child theme, ensure your parent theme's header.php does not define these variables in a way that breaks the child theme's override capabilities.

For developers moving beyond vanilla WordPress into frameworks like Laravel, the concept of "globals" is generally replaced by dependency injection or service containers, where data is passed explicitly from controller to view rather than relying on global state. If you are managing a custom PHP framework or a custom CMS structure similar to WordPress but without its hooks, applying the same singleton pattern for shared theme data remains a best practice to keep your codebase maintainable and testable.

Our network