For example, if I add this code to functions.php:


And then add this to one of my .php template files(like index.php or header.php):

<?php echo($mysite_address_url); ?>

It won't work. Why is this happens and how can I achieve this with wordpress please?

  • Use constants if a variable won't change: define('SITE', 'xyz'); echo SITE; – jgraup Feb 10 '16 at 13:02
  • Is it WordPress question? PHP echo – Max Yudin Feb 10 '16 at 14:38

WordPress theme files are called thru functions/classes, so variables declared in functions.php are not recognized by other theme files, until you specify you want to use the global value of the variable. Do so like this in your theme template files:

global $mysite_address_url;

you can then use $mysite_address_url as you'd like.

To read more about PHP's variable scope, see this

Also, if you want to use your website's URL, it'd be better to use the home_url() function

  • Don't create globals, stay out of global scope. There are far better solutons than greating globals just to sort a simple issue – Pieter Goosen Feb 10 '16 at 13:16

I recommend you to store it in WordPress options table using function


function as this is site specific data and not post/category specific.

  • Don't create globals, there are far better and more reliable methods to solve this issue. Do not create globals in order to solve a simple issue – Pieter Goosen Feb 10 '16 at 13:18
  • Pieter Goosen, can you write a better solution then please? – piopruua Feb 10 '16 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.