Solved

Why global variables don't work in WordPress template parts?

Posted on 2013-05-25
5
481 Views
Last Modified: 2013-05-25
Hi WP Experts!
I am relatively new to WP and coding a template part - I am finding that global variables do not work inside the template part and can't figure out why?

For example, please see the code below. The 'echo' statement displays an empty string, not the expected 'testing' value.

$myvar = "testing";
function test_func()
{
global $myvar;
echo $myvar;
}
test_func();

I Googled this condition and found that if I declare the variable $myvar as a global at the top of the page (even though it's the first time I'm using it), then assign the value of 'testing', the echo statement inside the test_func() then works as expected. But why?

Can anyone explain:

1) Why I need to declare the variable as a global in this case?
2) Is there a WP setting I can make somewhere centralized so that I don't need to worry about where globals work and where they don't (basically - so I can use global vars and have them work the way I expect them to work everywhere in the WP framework?)

Thanks experts!
0
Comment
Question by:rascal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 70

Accepted Solution

by:
Jason C. Levine earned 500 total points
ID: 39196895
1) There is a function buried deep in the WordPress core that unsets non-globalized variables as well as any variables named after a WordPress reserved word.  Your code above sets the variable before declaring it global so it gets nailed.

2) Not without hacking the core and that's frowned upon as there are better ways to work with globalized variables.  

http://betterwp.net/8-using-global-variables-in-wordpress/
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39196911
One possibility, if you really want global variables might be to use $GLOBALS which is an associative array and a superglobal PHP variable.  Most experienced programmers would not want global variables because of the risk of name collisions, and that's probably why WP goes out of its way to destroy any unknown variables.
0
 
LVL 1

Author Closing Comment

by:rascal
ID: 39196915
Thanks Jason!
0
 
LVL 1

Author Comment

by:rascal
ID: 39196917
Sorry Ray, I had marked the question as answered before I saw your post. Thank you very much for your input anyway!
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39196922
Not a problem at all - glad you've got a good answer!

Best to all, ~Ray
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
This article discusses how to implement server side field validation and display customized error messages to the client.
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question