admin管理员组

文章数量:1394081

I have a question on the following code at

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' );

Why does pluginprefix_setup_post_type need to be called on both init and activation? I tried an example plugin, commenting out the call to pluginprefix_setup_post_type in pluginprefix_install and adding the label like the following.

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' );

And the code works fine too. I can see the Books in the Admin menu once I activate the plugin.

So what is the benefit of calling it twice?

I have a question on the following code at https://developer.wordpress/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' );

Why does pluginprefix_setup_post_type need to be called on both init and activation? I tried an example plugin, commenting out the call to pluginprefix_setup_post_type in pluginprefix_install and adding the label like the following.

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' );

And the code works fine too. I can see the Books in the Admin menu once I activate the plugin.

So what is the benefit of calling it twice?

Share Improve this question asked Apr 9, 2020 at 2:58 Haibin LiuHaibin Liu 1333 bronze badges 1
  • When you say works fine you mean the permalink cache is set up correctly for the new type? I guess it's just being extra careful then in case the plugin activation code is called without the plugin being loaded in the original page load, because then it would miss the init hook. – Rup Commented Apr 9, 2020 at 8:46
Add a comment  | 

1 Answer 1

Reset to default 0

I think you're mistaken and the activation didn't work correctly: you won't have the new post type set up in the permalinks cache.

Here's activate_plugin() in 5.4. You can see the sequence is

  • wp-admin/plugins.php loads without the new plugin; init is triggered now
  • activate_plugin() PHP-includes the new plugin, loading it and registering its hooks; it's now too late for the plugin's init hook to be called
  • activate_plugin() calls the global activate_plugin hook
  • activate_plugin() calls the plugin-specific activate hook

So if you're relying on the init hook to register your post type in the activation case it won't have been called before flush_rewrite_rules(). Not that you'll see any errors here, but the new post type won't be set up in the permalink cache.

(That all is unless, on the very off chance you have an activate_plugin hook that triggers init, but that seems unlikely and definitely isn't the default.)

本文标签: plugin developmentA question on init and activation hooks