我对以下代码有疑问,请访问https://developer.wordpress.org/plugins/plugin-basics/activation-deactivation-hooks/#example
function pluginprefix_setup_post_type() {
// register the "book" custom post type
register_post_type( \'book\', [\'public\' => true] );
}
add_action( \'init\', \'pluginprefix_setup_post_type\' );
function pluginprefix_install() {
// trigger our function that registers the custom post type
pluginprefix_setup_post_type();
// clear the permalinks after the post type has been registered
flush_rewrite_rules();
}
register_activation_hook( __FILE__, \'pluginprefix_install\' );
为什么
pluginprefix_setup_post_type
两个都需要调用
init
和
activation
? 我尝试了一个示例插件,对调用
pluginprefix_setup_post_type
在里面
pluginprefix_install
并添加
label
如下所示。
function pluginprefix_setup_post_type() {
// register the "book" custom post type
register_post_type( \'book\', [\'public\' => true, \'label\' => \'Books\'] );
}
add_action( \'init\', \'pluginprefix_setup_post_type\' );
function pluginprefix_install() {
// trigger our function that registers the custom post type
// pluginprefix_setup_post_type();
// clear the permalinks after the post type has been registered
flush_rewrite_rules();
}
register_activation_hook( __FILE__, \'pluginprefix_install\' );
代码也运行良好。我可以看到
Books
在管理菜单中,一旦我激活插件。
那么,两次调用它有什么好处呢?