Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
wordpress插件-处理变量_Wordpress_Variables_Plugins - Fatal编程技术网

wordpress插件-处理变量

wordpress插件-处理变量,wordpress,variables,plugins,Wordpress,Variables,Plugins,我编写了一个简单的twitterconnect插件,需要在登录后在标题中显示“登录”用户的姓名。该函数工作正常,但我无法在标题中或函数外部的任何位置显示$user变量,即使它被分配为全局变量 以下是登录功能的结尾: $user= $Twitter->get_accountVerify_credentials(); print_r($user); // show screen name (not real name) $twitter_user = $user->screen_name

我编写了一个简单的twitterconnect插件,需要在登录后在标题中显示“登录”用户的姓名。该函数工作正常,但我无法在标题中或函数外部的任何位置显示$user变量,即使它被分配为全局变量

以下是登录功能的结尾:

$user= $Twitter->get_accountVerify_credentials();
print_r($user);
// show screen name (not real name)
$twitter_user = $user->screen_name;
// show profile image url
$twitter_image = $user->profile_image_url;
我可以看出它是成功的,因为$user被打印出来了,但是当我在header.php文件中以同样的方式调用它时,我可以看到一个错误:注意:未定义变量:user


有什么建议吗?

与被限制在变量范围内不同,我要编写一个函数来获取这些信息。比如:

function get_twitter_user_name(){
    $user= $Twitter->get_accountVerify_credentials();
    return $user->screen_name;
}
然后,在标题中,我想显示名称的地方,我会像这样调用函数:

<?php echo get_twitter_user_name(); ?>


根据代码的结构,这可能与我在这里看到的略有不同,但希望这将为您提供解决此问题的另一种方法。

您在哪个文件中声明了$Twitter变量?在函数中声明$twitter之前,很可能会调用header中的$user变量,并像这样挂接:add_action('init','twitter_logged_in');我相信标题是在这之后调用的。。