全局变量在WordPress插件中消失了


Global variables vanish in WordPress plugin

我在写一个WordPress插件。我想在保存帖子后显示自定义消息。此消息将取决于保存文章时调用的函数的结果。

下面是我的代码:

add_action('save_post', 'my_save_post_function');
function my_save_post_function() {
  global $msg;
  $msg = "Foo bar";
  ...
}
add_filter('post_updated_messages', 'my_post_updated_messages_function');
function my_post_updated_messages_function($messages) {
  global $msg;
  $messages["post"][1] = $msg; // !! $msg is undefined !!
  ...
}

为什么$msg未定义?

是否有任何方法我可以得到一个结果的save_post行动?我试过各种各样的把戏。甚至$_POST数据似乎已经被时间吹走了管理消息显示。

你试过会话了吗?我想你的问题会解决的。

看一看:

http://www.php.net/manual/en/function.session-start.php