douchuituo3032 2010-04-27 11:43
浏览 56
已采纳

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
  • 写回答

1条回答 默认 最新

  • dongrong9938 2010-04-27 14:40
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ssh登录页面的问题
  • ¥60 渗透一个指定银行app,拿到客户信息,需要什么级别
  • ¥50 关于在matlab上对曲柄摇杆机构上一点的运动学仿真
  • ¥15 jetson nano
  • ¥15 :app:debugCompileClasspath'.
  • ¥15 windows c++内嵌qt出现数据转换问题。
  • ¥20 公众号如何实现点击超链接后自动发送文字
  • ¥15 用php隐藏类名和增加类名
  • ¥15 算法设计与分析课程的提问
  • ¥15 用MATLAB汇总拟合图