Custom role, capabilities, and post type: preview button wrecks things

Question

I’m using the Members plugin to manage capabilities of a custom role that will only be able to edit/publish a custom post type that I created in my functions.php file. After much work, I got this working, except the preview button does not work and (what’s worse) prevents saving/publishing custom posts.

Everything works fine as long as I do not click the preview button. But when I click the preview button the preview tab does not show any of the changes I’ve made since the last save. Then when I press the publish/update button, my changes are not saved/published, but are loaded into the preview tab, as if I had pressed preview. Nothing else happens in the browser tab where I’m using WordPress to edit the post, the spinner next to the update/publish button just keeps spinning.

Everything works fine from my admin account, and if I give the custom role the “edit_posts” capability that solves it. So it looks like something is going wrong with the custom capabilities for this role, but I can’t figure it out.

Below is my code from functions.php. Thanks for any suggestions.

// Register Custom Post Type
function mnp_custom_post_type() {
$labels = array(
    'name'                => 'Wiki Pages',
    'singular_name'       => 'Wiki Page',
    'menu_name'           => 'Wiki Pages',
    'parent_item_colon'   => 'Parent Wiki Page:',
    'all_items'           => 'All Wiki Pages',
    'view_item'           => 'View Wiki Page',
    'add_new_item'        => 'Add New Wiki Page',
    'add_new'             => 'New Wiki Page',
    'edit_item'           => 'Edit Wiki Page',
    'update_item'         => 'Update Wiki Page',
    'search_items'        => 'Search wiki pages',
    'not_found'           => 'No wiki pages found',
    'not_found_in_trash'  => 'No wiki pages found in Trash',
);

$rewrite = array(
    'slug'                => 'wiki',
    'with_front'          => false,
    'pages'               => true,
    'feeds'               => true,
);

$capabilities = array(
    'edit_post'           => 'edit_wiki_page',
    'read_post'           => 'read_wiki_page',
    'delete_post'         => 'delete_wiki_page',
    'edit_posts'             => 'edit_wiki_pages',
    'edit_others_posts'      => 'edit_others_wiki_pages',
    'publish_posts'          => 'publish_wiki_pages',
    'read_private_posts'     => 'read_private_wiki_pages',
    'delete_posts'           => 'delete_wiki_pages',
    'delete_private_posts'   => 'delete_private_wiki_pages',
    'delete_published_posts' => 'delete_published_wiki_pages',
    'delete_others_posts'    => 'delete_others_wiki_pages',
    'edit_private_posts'     => 'edit_private_wiki_pages',
    'edit_published_posts'   => 'edit_published_wiki_pages',
);

$args = array(
    'label'               => 'wiki_page',
    'description'         => 'Wiki pages',
    'labels'              => $labels,
    'supports'            => array( 'title', 'editor', 'revisions', ),
    'taxonomies'          => array( 'category', 'post_tag' ),
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 25,
    'menu_icon'           => '',
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'rewrite'             => $rewrite,
    'capabilities'        => $capabilities,
);

register_post_type( 'wiki_page', $args );
}

// Hook into the 'init' action
add_action( 'init', 'mnp_custom_post_type', 0 );

// map meta capabilities
add_filter( 'map_meta_cap', 'my_map_meta_cap', 10, 4 );

function my_map_meta_cap( $caps, $cap, $user_id, $args ) {

/* If editing, deleting, or reading a wiki_page, get the post and post type object. */
if ( 'edit_wiki_page' == $cap || 'delete_wiki_page' == $cap || 'read_wiki_page' == $cap ) {
    $post = get_post( $args[0] );
    $post_type = get_post_type_object( $post->post_type );

    /* Set an empty array for the caps. */
    $caps = array();
}

/* If editing a wiki_page, assign the required capability. */
if ( 'edit_wiki_page' == $cap ) {
    if ( $user_id == $post->post_author )
        $caps[] = $post_type->cap->edit_posts;
    else
        $caps[] = $post_type->cap->edit_others_posts;
}

/* If deleting a wiki_page, assign the required capability. */
elseif ( 'delete_wiki_page' == $cap ) {
    if ( $user_id == $post->post_author )
        $caps[] = $post_type->cap->delete_posts;
    else
        $caps[] = $post_type->cap->delete_others_posts;
}

/* If reading a private wiki_page, assign the required capability. */
elseif ( 'read_wiki_page' == $cap ) {

    if ( 'private' != $post->post_status )
        $caps[] = 'read';
    elseif ( $user_id == $post->post_author )
        $caps[] = 'read';
    else
        $caps[] = $post_type->cap->read_private_posts;
}

/* Return the capabilities required by the user. */
return $caps;
}
0
, , Paul Morris 9 years 2013-03-12T10:42:27-05:00 0 Answers 63 views 0

Leave an answer

Browse
Browse