Php Wordpress';插件开发:register\u activation\u hook()不使用register\u deactivation\u hook()?

Php Wordpress';插件开发:register\u activation\u hook()不使用register\u deactivation\u hook()?,php,wordpress,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_opti

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(),从而取消了我在激活中所做的操作,我认为它不应该这样做。我对此非常着迷。

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

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

WP Codex reference:

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

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

WP Codex参考资料:

哦,糟了,我早就应该注意到了!谢谢我的救命恩人!糟糕,我早该注意到的!谢谢我的救命恩人!