WordPress清除$ 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 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
我想要做的是在Word Press主题中包含我的一个PHP脚本。 问题是,在我包含脚本文件后,我无法访问,在主题文件中的内部函数,脚本文件中声明的变量。 p>
我在主题中创建了一个新文件 文件夹并添加了与header.php中相同的代码,如果我打开该文件它就可以正常工作。 因此,据我所知,Word Press与之相关。 p>
/other/path/wordpress/wp-content/themes/theme-name/header.php // this 破坏了
/ other / path / wordpress / wp-content / themes / theme-name / test.php //这个工作
/ var / www / vhosts / domain / wordpress / - >(符号链接) - > / other / path / wordpress /
/other /path/wordpress/wp-content/themes/theme-name/header.php
/var/www/vhosts/domain/include_file.php
code>
内容:/var/www/vhosts/domain/include_file.php
nn$global_var ='global';
print_r($ GLOBALS); //如果我直接打开这个文件,打印全局符号$ global_var;
//如果这个文件包含在头文件中,这将打印所有WP内容而不用$ global_var;
code> pre>
内容:/other/path/wordpress/wp-content/themes/theme-name/header.php
require'/path/to/include_file.php';
nn print $ global_var; //按预期打印'全局'
函数测试()
\ {
全局$ global_var;
打印$ global_var; //这是NULL
}
test();
print_r($ GLOBALS); //这会在其中打印所有没有$ global_var的WP内容
code> pre>
div>
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.