When you create a custom post type, does that also create capabilities for editing/deleting that post type automatically?

For example, if I create a post type called “destinations” does that automatically create capabilities like “edit_destinations” or “delete_destinations”?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

It does not automatically create that capability in the sense that there is no new capability registered with WordPress. Instead it defaults to use the capabilities assigned to creating/editing posts. For example, if an author logs in they will be able to create and publish a new destination entry by default.

You can override this with the capabilities value when using register_post_type. See Justin Tadlock’s excellent tutorial here http://justintadlock.com/archives/2010/04/29/custom-post-types-in-wordpress

Method 2

Define a central variable for the custom post type: public $post_type_1 = 'archiv';

and use this on add new capabilities:

        $capabilities = array(
            'edit_post'          => 'edit_' . $this->post_type_1,
            'edit_posts'         => 'edit_' . $this->post_type_1 . 's',
            'edit_others_posts'  => 'edit_others_' . $this->post_type_1 . 's',
            'publish_posts'      => 'publish_' . $this->post_type_1 . 's',
            'read_post'          => 'read_' . $this->post_type_1,
            'read_private_posts' => 'read_private_' . $this->post_type_1 . 's',
            'delete_post'        => 'delete_' . $this->post_type_1
        );

Also i add this new capabilties objects to the differnet default roles, only on activation of the plugin:

        foreach ( $this->todo_roles as $role ) {
            $wp_roles->add_cap( $role, 'edit_'          . $this->post_type_1 );
            $wp_roles->add_cap( $role, 'edit_'          . $this->post_type_1 . 's' );
            $wp_roles->add_cap( $role, 'edit_others_'   . $this->post_type_1 . 's' );
            $wp_roles->add_cap( $role, 'publish_'       . $this->post_type_1 . 's' );
            $wp_roles->add_cap( $role, 'read_'          . $this->post_type_1 );
            $wp_roles->add_cap( $role, 'read_private_'  . $this->post_type_1 . 's' );
            $wp_roles->add_cap( $role, 'delete_'        . $this->post_type_1 );
            $wp_roles->add_cap( $role, 'manage_'        . $this->taxonomy_type_1 );
        }

        foreach ( $this->read_roles as $role ) {
            $wp_roles->add_cap( $role, 'read_' . $this->post_type_1 );
            $wp_roles->add_cap( $role, 'read_' . $this->post_type_1 );
            $wp_roles->add_cap( $role, 'read_' . $this->post_type_1 );
        }

        global $wp_rewrite;
        $wp_rewrite->flush_rules();

But, you must also unregister this objects, when the plugin will be uninstalled.

You can see an example on this gist: https://gist.github.com/978690


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x