Wordpress的插件开发:register_activation_hook()不能与register_deactivation_hook()一起使用?

时间:2012-11-15 09:16:30

标签: php wordpress

Wordpress版本3.4.2

这是最简单的插件形式,只有两个钩子:

register_activation_hook(__FILE__, sbv_activate());
register_deactivation_hook(__FILE__, sbv_deactivate());

function sbv_activate() {
  add_option('sbv_opt1', 'test');
}

function sbv_deactivate() {
  delete_option('sbv_opt1');
}

我在激活此插件后去检查选项表,似乎该选项不存在,如果我从去激活功能中删除delete_option()行,它将起作用。纠正我,如果我错了,似乎wordpress正在调用sbv_activate()然后调用sbv_deactivate(),从而撤消我在激活中所做的事情,我不认为它应该像这样。我对此感到很疯狂。

1 个答案:

答案 0 :(得分:3)

您应该只使用函数的名称而不是函数本身:

register_activation_hook(__FILE__, 'sbv_activate');
register_deactivation_hook(__FILE__, 'sbv_deactivate');

WP Codex参考:http://codex.wordpress.org/Function_Reference/register_activation_hook