'Wordpress PHP using global constants to avoid hard-coded values
I am developing on a Wordpress website with my own php coding. So far I am using the Snippets plugin which I like most for adding PHP code to existing wordpress sites.
The only thing I would like to know is how i can use something like global constants to avoid hard-coded values. Because I am using the same values again and again. What is the best way?
Thank you for any help. best,
Solution 1:[1]
It is not recommended to create your own global variables on Wordpress (read this). But you you can achieve this by defining global variables.
function my_globals() {
global $myglobals;
// We define it as an array so you can store multiple values. If only needed one value you can directly set it
$myglobals = array();
$myglobals['first'] = 'This is first content';
$myglobals['second'] = 'This is second content';
}
add_action( 'after_setup_theme', 'my_globals' );
Now you can call your global using:
global $myglobals;
echo $myglobal['first'];
echo $myglobal['last'];
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
