Solved

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

Posted on 2013-05-25
5
436 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
  • 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 109

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 109

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

WordPress is constantly evolving, and with each evolution appears to get better and better.  One of the big drawbacks prior to version 3 was that there was no way to be able to set up a custom menu from the backend. The Old Way Adding menus is…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…
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…

832 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