0

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

$mysite_address_url="http://my-site.com";

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?

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

2 Answers 2

1

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

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

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

add_option() 

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

2
  • 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 Feb 10, 2016 at 13:18
  • Pieter Goosen, can you write a better solution then please?
    – piopruua
    Feb 10, 2016 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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