Solved

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

Posted on 2013-05-25
5
455 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…

730 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