Solved

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

Posted on 2013-05-25
5
425 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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
Thanks Jason!
0
 
LVL 1

Author Comment

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

Expert Comment

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

Best to all, ~Ray
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

WordPress can be pretty daunting, especially for a beginner, so I thought it might be a good idea to write an article to show how easy it is to get started in WordPress and to design a custom theme.  The first step is to check with your hosting comp…
This article discusses four methods for overlaying images in a container on a web page
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
The viewer will learn how to count occurrences of each item in an array.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now