WordPress.org

Forums

[resolved] Bad preview button glitch with custom role/post type/capabilities (4 posts)

  1. PaulMorris
    Member
    Posted 2 years ago #

    I've created a custom post type, and a custom role to go with it. Those with this role will only be able to edit the custom posts.

    I've got this pretty much working by coding the custom post type in functions.php and then using the Members plugin to create the new role and assign it just the capabilities to edit the custom posts.

    I am getting some very strange behavior with the preview button though. I log in with the custom role, and I can create, open, and edit posts. If I do not click the preview button I can publish my edits to the post (or publish a new post). However (and here's where it gets weird) if I click the preview button a tab opens with a preview that does not show any of the changes I've made since the last save. Then when I press the publish or update button, my changes are not saved or published, but are loaded into the "preview" browser tab. Nothing else happens in the browser tab where I'm using WordPress to edit the post, the spinny spinner just keeps spinning.

    Everything works fine from my admin account, so it looks like something is amiss in the custom role / capabilities dept rather than with the custom post type.

    Anyone run into this before? Any ideas on how to fix it? My code from functions.php is below.

    Thanks,
    -Paul

    // 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', 'author', '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 movie, 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 movie, 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 movie, 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 movie, 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;
    }

  2. PaulMorris
    Member
    Posted 2 years ago #

    More clues: if I give the custom role the capability to "edit_posts" then everything works fine. Without it, preview does not work as described above (even though the custom role has the capability to "edit_wiki_pages").

  3. PaulMorris
    Member
    Posted 2 years ago #

    I tried creating a completely fresh test install of WordPress, and setting up the custom role, capabilities, and post type from the functions.php file (no plugins used), to see if this would correct the problem.

    It didn't. I got the same behavior with the preview button, while everything else worked as it should. This leads me to believe it may be a bug in WordPress rather than something I am doing wrong.

    I hate to have to resort to this, but I think I will have to try hiding the preview button with CSS, hopefully just for those custom users. The new code I used is below.


    // Register Custom Post Type
    function my_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,
    );

    $args = array(
    'label' => 'wpage',
    '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,
    'can_export' => true,
    'has_archive' => true,
    'exclude_from_search' => false,
    'publicly_queryable' => true,
    'rewrite' => $rewrite,
    'capability_type' => 'wpage',
    'map_meta_cap' => true,

    );

    register_post_type( 'wpage', $args );
    }
    // Hook into the 'init' action
    add_action( 'init', 'my_custom_post_type', 0 );

    // set up custom role and custom capabilities
    if ( is_admin() && '1' == $_GET['reload_caps'] ) {

    if ( get_role( 'wiki_author' ) == null) {
    add_role('wiki_author', 'Wiki Author', array(
    'read' => true,
    ));
    }

    $administrator = get_role('administrator');
    $editor = get_role('editor');
    $wiki_author = get_role('wiki_author');

    $wiki_author->add_cap( 'upload_files' );

    foreach ( array('publish','delete','delete_others','delete_private','delete_published','edit','edit_others','edit_private','edit_published','read_private') as $cap ) {
    $administrator->add_cap( "{$cap}_wpages" );
    $editor->add_cap( "{$cap}_wpages" );
    $wiki_author->add_cap( "{$cap}_wpages" );
    }
    }

  4. PaulMorris
    Member
    Posted 2 years ago #

    It's working now. I'm not sure why or how.

    I tried applying the workaround to bug 19378 that's posted in comment 6 of this page:
    http://core.trac.wordpress.org/ticket/19378

    Since it looked related. It didn't seem to have an effect immediately, but maybe that's what did it?

Topic Closed

This topic has been closed to new replies.

About this Topic