【发布时间】:2012-03-05 20:16:35
【问题描述】:
我正在编写一个动态创建字符串的 Wordpress 插件。
我想使用wp_head 钩子将此字符串插入到元标记中。
我的插件的工作方式如下:我定义了一个短代码,其处理函数声明了一个 add_action(...,它将一个特殊的 <meta> 标记添加到标题中。
这行得通,但是...
我唯一的问题是我不知道如何传递包含字符串的变量以打印到头部。该变量未定义,即使我在插件中对其进行了全球化。
//INSIDE MY PLUGIN...
global $head_string;
function initialize_my_plugin_class() {
$head_string = "<meta>bla bla bla</meta>";
}
function add_action('wp_head', 'add_meta_tags' ) //this is slightly oversimplified
//the execution order of wp_head makes
//the real code a bit more complicated
//for the purposes of this question let's
//pretend it's like this.
function add_meta_tags() {
echo $head_string
}
结果有效,除了 $head_string 变量为空。所以它打印一个空的元标记。 (我知道其他一切都有效,因为我可以更改 add_meta_tags() 以执行类似 echo "FAKE META TAG"; 的操作,并且它会显示在标题中的应有位置。)
那么我在做什么有什么问题吗?我认为它必须涉及变量范围,但我被卡住了。
【问题讨论】: