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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失