D
D
damarkuzz2021-09-16 21:06:13
PHP
damarkuzz, 2021-09-16 21:06:13

How to set a variable whose value can be displayed anywhere on the site?

Tell me how to set a single variable, the value of which can be displayed anywhere on the site (header.php, footer.php, etc.)?
If I specify this in functions.php, for example, as $city = 'Москва';, and then call in header.php echo $city;, then empty space is displayed.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
egor_nullptr, 2021-09-16
@damarkuzz

Instead of $city use $GLOBALS['city'], the $GLOBALS variable is available everywhere.

D
Developer, 2021-09-16
@samodum

Singleton
But it is not correct.
Use a common class for the entire site.
Or settings in xml, gradle and the like

A
Artem Zolin, 2021-09-16
@artzolin

Another option is to useupdate_option()

$city_data = array(
  'iso-area' => 'RU-MOS',
  'title' => 'Москва',
  'gde' => 'в Москве',
  'kuda' => 'в Москву',
  'otkuda' => 'из Москвы',
  'chej' => 'Московский',
  'chego' => 'Москвы',
  'chemu' => 'Москве',
  'o-chem' => 'о Москве',
  'english' => 'Moscow',
  'population' => '12 655 050',
  'region' => 'central',
);

update_option( '_city_data', $city_data, 'yes' );

You can once put the data you need in the site options and receive it in any place you need to use it
$site_data = get_option( '_site_data' );
vardump( $site_data['title'] );

The last argument of the yes / no function means loading data on autoload, i.e. its value will be automatically loaded into memory when the engine is loaded, and when accessed, it will be taken from the cache
. If set to no , then accessing the option will create an additional request to the database. Choose based on how often you need this option in a project

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question