• Resolved Daniel P.

    (@danidub)


    Hello, I’m trying to understand the plugin handbook example on registering a custom post type inside the activation hook:

    /**
     * Register the "book" custom post type
     */
    function pluginprefix_setup_post_type() {
        register_post_type( 'book', ['public' => true ] ); 
    } 
    add_action( 'init', 'pluginprefix_setup_post_type' );
     
     
    /**
     * Activate the plugin.
     */
    function pluginprefix_activate() { 
        // Trigger our function that registers the custom post type plugin.
        pluginprefix_setup_post_type(); 
        // Clear the permalinks after the post type has been registered.
        flush_rewrite_rules(); 
    }
    register_activation_hook( __FILE__, 'pluginprefix_activate' );

    Why is the pluginprefix_activate function calling pluginprefix_setup_post_type if this is already being added to init with the add_action function?

    The example works the same if the function is not called inside pluginprefix_activate so this is confusing me. I understand that is important to call flush_rewrite_rules on activation but not pluginprefix_setup_post_type

    Any ideas?
    Thank you.

    • This topic was modified 3 years, 10 months ago by Daniel P..
Viewing 3 replies - 1 through 3 (of 3 total)
  • Why is the pluginprefix_activate function calling pluginprefix_setup_post_type if this is already being added to init with the add_action function?

    If the plugin is not activated, its action would not be called for the init action.

    Moderator bcworkz

    (@bcworkz)

    It’s only registered on activation so that the rewrite rules can be updated. You don’t want to update rewrite rules on every request, though registering the post type on every request is necessary.

    Thread Starter Daniel P.

    (@danidub)

    @bcworkz ok great, thank you!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Docs on registering CPT on plugin activation hook’ is closed to new replies.