Does WordPress clear $GLOBALS?
What I want to do is to include one of my PHP scripts in a Word Press theme. The problem is that after I include the script file I can't开发者_C百科 access, inside functions in the theme file, variables declared in the script file .
I have created a new file in the theme folder and added the same code as in header.php and if I open that file it works just fine. So as far as I can tell it's something Word Press related.
/other/path/wordpress/wp-content/themes/theme-name/header.php // this is broken
/other/path/wordpress/wp-content/themes/theme-name/test.php // this works
/var/www/vhosts/domain/wordpress/ ->(symlink)-> /other/path/wordpress/
/other/path/wordpress/wp-content/themes/theme-name/header.php
/var/www/vhosts/domain/include_file.php
Content of: /var/www/vhosts/domain/include_file.php
$global_var = 'global';
print_r($GLOBALS); // if I open this file directly this prints globals WITH $global_var;
// if this file is included in header this prints all the WP stuff WITHOUT $global_var;
Content of: /other/path/wordpress/wp-content/themes/theme-name/header.php require '/path/to/include_file.php';
print $global_var; // this prints 'global' as expected
function test()
{
global $global_var;
print $global_var; // this is NULL
}
test();
print_r($GLOBALS); // this prints all the WP stuff WITHOUT $global_var in it
I'm not promoting the use of $GLOBALS
, but anyway... define your variable using:
$GLOBALS['varname'] = 'value';
That should work. I suspect you're not actually in global scope like you think you are. That file is probably being include by a function, in which case you're in function-scope.
精彩评论