Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

wordpress

In wordpress, it calls header.php then whatever template name or the page requested. How can I declare a variable in my php header which I can then refer to in my other templates?

like image 484
Claire Avatar asked Sep 28 '12 11:09

Claire


People also ask

How do I create a global variable in PHP?

So, a global variable can be declared just like other variable but it must be declared outside of function definition. Syntax: $variable_name = data; Below programs illustrate how to declare global variable.

How can we create a variable fo to be used globally across all models?

The best way to share global variables across modules across a single program is to create a config module. Just import the config module in all modules of your application; the module then becomes available as a global name.

Which is used to access global variables from anywhere in the PHP script?

$GLOBALS is a PHP super global variable which is used to access global variables from anywhere in the PHP script (also from within functions or methods). PHP stores all global variables in an array called $GLOBALS[index].


3 Answers

You can declare it like so (In functions.php):

global $domain; $domain = 'http://www.mydomain.com/'; 

You can echo the domain of the WordPress site like this, though..

<?php bloginfo('site_url');?> 

Or

$url = site_url(); echo $url; 

Or as a function, in functions.php

function displayMyDomain() {   global $domain; // you probably don't actually need to set it global as it is a function   $domain = 'http://www.domain.com/';   echo $domain; } 

Then anywhere, use <?php displayMyDomain(); ?>

like image 30
SMacFadyen Avatar answered Sep 29 '22 10:09

SMacFadyen


The answer pretty much depends what you need it for. If you are developing a theme and want to keep values constant through all files you can place them in functions.php in the theme directory, which is always loaded. Variables defined there should be available everywhere in the theme. This works if you distribute the theme.

If you want to modify an existing theme for your needs on your own installation, you can either put them in wp-config.php, as suggested, or (a cleaner method) you can create a child theme of the theme you want to change. This will keep it separate from the wordpress core and will prevent theme updates from overwriting your changes.

I just tried it using functions.php:

functions.php:

$variable = "value"; 

header.php:

global $variable; echo $variable; 

works for me.

like image 194
Gerald Schneider Avatar answered Sep 29 '22 12:09

Gerald Schneider


You can define them in wp-config.php file.

The config file is never updated so your Wordpress installation will have those variables over time even if you update it.

<?php
  require_once('wp-config.php');
  echo $domain;
?>
like image 32
Mihai Iorga Avatar answered Sep 29 '22 11:09

Mihai Iorga