我想要做的是在Word Press主题中包含我的一个PHP脚本。问题是,在我包含脚本文件后,我无法访问,在主题文件中的内部函数,脚本文件中声明的变量。
我在主题文件夹中创建了一个新文件,并添加了与header.php相同的代码,如果我打开该文件就可以了。所以据我所知,Word Press与之相关。
/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
内容:/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;
内容:/other/path/wordpress/wp-content/themes/theme-name/header.php 要求'/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
答案 0 :(得分:4)
我不推广使用$GLOBALS
,但无论如何......使用以下方法定义您的变量:
$GLOBALS['varname'] = 'value';
那应该有用。我怀疑你实际上并不像你认为的那样在全球范围内。该文件可能包含在一个函数中,在这种情况下,您处于函数范围内。