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; Commented Feb 10, 2016 at 13:02
  • Is it WordPress question? PHP echo Commented 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 Commented 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 Commented Feb 10, 2016 at 13:18
  • Pieter Goosen, can you write a better solution then please? Commented Feb 10, 2016 at 13:19

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.