admin管理员组文章数量:1122846
My custom PHP script works on my PC over localhost. But, when I upload my code to the 'live' WordPress site, The $GLOBALS
values are just null
even they work on my localhost. (Nothing wrong with the database connection). Is the $GLOBALS
not working on WordPress? My code is something like this one below:
$get_value = $db->query("SELECT * FROM mytable")->fetch();
$get_variable = $get_value['myvalue'];
function myfunction(){
return $GLOBALS['get_variable'];
}
My custom PHP script works on my PC over localhost. But, when I upload my code to the 'live' WordPress site, The $GLOBALS
values are just null
even they work on my localhost. (Nothing wrong with the database connection). Is the $GLOBALS
not working on WordPress? My code is something like this one below:
$get_value = $db->query("SELECT * FROM mytable")->fetch();
$get_variable = $get_value['myvalue'];
function myfunction(){
return $GLOBALS['get_variable'];
}
Share
Improve this question
edited May 4, 2018 at 14:49
Mayeenul Islam
12.9k21 gold badges85 silver badges169 bronze badges
asked May 4, 2018 at 14:43
mage_entomage_ento
112 bronze badges
1
|
1 Answer
Reset to default 0The correct syntax for using with global keyword. To access a global variable in WordPress, you first need to globalize the variable with global $variable;
Write inside function.php as:
function myfunction(){
global $get_variable;
$get_value = $db->query("SELECT * FROM mytable")->fetch();
$get_variable = $get_value;
}
add_action( 'after_theme_setup', 'myfunction' );
Inside the function you can now able to any where. But, outside of this scope it needs to be re-declared as a global scope variable. Ex., to use inside single.php file, it will work as
global $get_variable;
echo $get_variable;
本文标签: GLOBALS39value139 is not working
版权声明:本文标题:$GLOBALS['value1'] is not working 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736294767a1929428.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$GLOBALS['get_variable'] = ...
to set it. – mmm Commented May 11, 2018 at 18:29