0

I have created a custom post type "Extension" and a role "Extension Author". A user in this role should only be able to edit his/her own extensions.

As it is now the user can click "edit extension" in top bar and enter edit mode.

Edit Extension Link

How do i prevent an Extension Author from entering edit view of extensions not his/her own? Have I missed something when setting capabilities?

Creating custom post type:

$labels = array(
    'name'                => _x( 'Extensions', 'Post Type General Name', 'text_domain' ),
    'singular_name'       => _x( 'Extension', 'Post Type Singular Name', 'text_domain' ),
    'menu_name'           => __( 'Extensions', 'text_domain' ),
    'parent_item_colon'   => __( 'Parent Extension:', 'text_domain' ),
    'all_items'           => __( 'All Extensions', 'text_domain' ),
    'view_item'           => __( 'View Extension', 'text_domain' ),
    'add_new_item'        => __( 'Add New Extension', 'text_domain' ),
    'add_new'             => __( 'Add New', 'text_domain' ),
    'edit_item'           => __( 'Edit Extension', 'text_domain' ),
    'update_item'         => __( 'Update Extension', 'text_domain' ),
    'search_items'        => __( 'Search Extension', 'text_domain' ),
    'not_found'           => __( 'No Extensions found', 'text_domain' ),
    'not_found_in_trash'  => __( 'No Extensions found in Trash', 'text_domain' ),
);
$rewrite = array(
    'slug'                => 'extension',
    'with_front'          => true,
    'pages'               => true,
    'feeds'               => true,
);
$capabilities = array(
    'edit_post'           => 'edit_extension',
    'read_post'           => 'read_extension',
    'delete_post'         => 'delete_extension',
    'edit_posts'          => 'edit_extensions',
    'edit_others_posts'   => 'edit_others_extensions',
    'publish_posts'       => 'publish_extensions',
    'read_private_posts'  => 'read_private_extensions',
);
$args = array(
    'labels'              => $labels,
    'supports'            => array( 'title',),
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 5,
    'menu_icon'           => 'dashicons-admin-generic',
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'rewrite'             => $rewrite,
    'capability_type'     => 'extension',
    'capabilities' => $capabilities,
);

register_post_type( 'extension', $args );

Creating user role:

    $extension_author_role = add_role('extension_author', 'Extension Author', array(
    'read' => true,
    'upload_files' => true,
    'edit_extension'   => true,
    'read_extension'   => true,
    'delete_extension'   => true,
    'edit_extensions'   => true,
    'edit_others_extensions'   => false,
    'publish_extensions'   => true,
    'read_private_extensions'   => false,
    )
);
0

Using this code snippet makes it that an author will see now only his/her own posting
and is only able to edit his/her own postings. Only Admins will now be able to edit all posting.

You could try to replace 'author' with 'extension_author' and see if it works for you as wished. (Add snippet in functions.php or your own plugin)

function to_parse_query_useronly( $wp_query ) {
    if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/edit.php' ) !== false ) {
        if ( !current_user_can( 'publish_posts' ) ) {
            global $current_user;
            $wp_query->set( 'author', $current_user->id );
        }
    }
}
add_filter('parse_query', 'to_parse_query_useronly' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.