这可能不是这个问题的直接答案。然而,我已经看到了处理此问题的多种变通方法(flush_rewrite_rules()
首先执行)。
解决方法是设置标志(使用add_option()
) 然后钩住init
, 如果标志存在,则刷新重写规则。有点恶心。
如果你想注册的任何东西都可以在插件激活后注册,你可以尝试另一种方法。
register_activation_hook()
在内部注册操作挂钩activate_PLUGIN_NAME
. 因此,您可以做的是以更高的优先级在同一挂钩上注册代码。
例如:
// __FILE__ is the main plugin file that contains the plugin headers.
$activation_hook = plugin_basename( __FILE__ );
// Register your stuff here, using a lower priority.
add_action( "activate_{$activation_hook}", "your_callback_to_register", 1, 1 );
现在,您可以使用
register_activation_hook()
免费,因为它将以更高的优先级运行。尽管要注意,这个钩子注册某些东西可能为时已晚,但它可以很好地为插件激活添加重写规则。