使用activation\\u挂钩不是最好的主意。原因如下(让我们看看Codex对激活挂钩的说法):
当插件被激活时,会调用action“activate\\u PLUGINNAME”挂钩。在这个钩子的名称中,PLUGINNAME被替换为插件的名称,包括可选的子目录。例如,当插件位于wp-content/plugin/sampleplugin/sample中时。php,则此挂钩的名称将变为“activate\\u sampleplugin/sample”。php’。当插件仅包含一个文件且(默认情况下)位于wp content/plugin/sample时。php此挂钩的名称为“activate\\u sample”。php’。
您可以在此处阅读有关完整流程的更多信息:https://codex.wordpress.org/Function_Reference/register_activation_hook#Process_Flow
这意味着activation hook is run only once - 当插件被激活时,因此,在每个其他请求期间,when the plugin is already active, your taxonomy won\'t get registered, 所以WordPress不会看到/理解它。
当然,当插件处于活动状态时,您可以注册分类法,以便在每个请求期间都可以使用它。
所以是的-using init action is much better idea... 和Codex is clearly suggesting that:
使用init操作调用此函数。在操作之外调用它可能会导致麻烦。看见#15568 有关详细信息。