Custom post type – no layout section of Document tab, and no author choice

Question

A portion of my website is used by another non-profit organization. In order to restrict them from editing/deleting at large, I created a custom post type for them, and added a user role to go with it, using the excellent model presented by WebElaine

It basically works: they can get into their custom post type in the backend and create/edit pages, they are the author, they can’t get into my pages, and basically all good.

A couple of odd things.

  1. Although they are the author of the pages they create, there is no “Author” section of the Document tab in the editor, in case I wanted to turn a page over to them or vice versa. When I list all pages of the special type, and do a Quick Edit, there is an author drop-down, but they are not in it, even for pages where they are author.

  2. In the editor, Documents tab, there is no “Layout” section that allows using sidebars or not (for them; there is for me). Maybe because that is controlled by the theme (GeneratePress), which is unaware of this post type?

Here is the plugin that does the custom post type and user role.

/* Plugin Name: fp custom post types
*/

/********************************************************/
/*   Function to create the custom post type 'ncpage'   */
/********************************************************/

function fp_create_post_type() {
    // Capabilities of custom post type
    // First three are meta capabilities
    $capabilities = array(
        'edit_post'                 => 'edit_ncpage', 
        'publish_post'              => 'publish_ncpage', 
        'delete_post'               => 'delete_ncpage', 
        'publish_pages'             => 'publish_ncpages',
        'edit_pages'                => 'edit_ncpages',
        'edit_published_pages'      => 'edit_published_ncpages',
        'edit_others_pages'         => 'edit_others_ncpages',
        'edit_private_pages'        => 'edit_private_ncpages',
        'delete_pages'              => 'delete_ncpages',
        'delete_private_pages'      => 'delete_private_ncpages',
        'delete_published_pages'    => 'delete_published_ncpages',
        'delete_others_pages'       => 'delete_others_ncpages',
        'read_private_pages'        => 'read_private_ncpages'
    );
    
    // Set backend UI labels for Custom Post Type
    $labels = array(
        'name'                => ( 'NCpages' ),
        'singular_name'       => ( 'NCpage' ),
        'menu_name'           => ( 'NCFPW Pages' ),
        'parent_item_colon'   => ( 'Parent NCpage' ),
        'all_items'           => ( 'All NCpages' ),
        'view_item'           => ( 'View NCpage' ),
        'add_new_item'        => ( 'Add New NCpage' ),
        'add_new'             => ( 'Add New' ),
        'edit_item'           => ( 'Edit NCpage' ),
        'update_item'         => ( 'Update NCpage' ),
        'search_items'        => ( 'Search NCpage' ),
        'not_found'           => ( 'Not Found' ),
        'not_found_in_trash'  => ( 'Not found in Trash' )
    );

    // Add other CPT arguments
    $args = array(
        'labels'                => $labels,
        'capabilities'          => $capabilities,
        // Features this CPT supports in Post Editor
        'supports'              => array ( 
                  'title', 'editor', 'comments', 'revisions', 
                  'author', 'page-attributes', 
                  'thumbnail', 'custom-fields', 'post-formats' ),
        'description'           => 'NCFPW pages for corresponding user role',
        'map_meta_cap'          => true,
        'capability_type'       => 'ncpage',
        // Set rewrite to desired URL, otherwise
        // it defaults to true and uses $post-type as slug (ncpage)
        'rewrite'               => array('slug' => 'ncfpw'),
        // Other arguments
        // show_ui, show_in_nav_menus, and publicly queryable default to $public, 
        // show_in_menu defaults to $show_ui
        // show_in_admin_bar defaults to $show_in_menu
        'public'                => true,
        'menu_position'         => 4,
        'hierarchical'          => true,  // can have parent and child, like pages
        'show_in_rest'          => true,  // block editor support
        'can_export'            => true,
        'exclude_from_search'   => false
    );
    
    // Actually create the post type.  First is the post-type name ($post_type)
    register_post_type('ncpage', $args);
}

/************************************/
/*        Call the function         */
/************************************/

add_action('init', 'fp_create_post_type');

/*********************************************************/
/* Add user role, access to ONLY ncpage and own images   */
/*********************************************************/
function fp_add_ncfpw_role() {
    add_role('ncfpw_author', 'NCFPW Author', array(
        // Custom capabilities
        'publish_ncpages'          => true,
        'edit_ncpages'             => true,
        'edit_others_ncpages'      => true,
        'edit_private_ncpages'     => true,
        'edit_published_ncpages'   => true,
        'delete_ncpages'           => true,
        'delete_published_ncpages' => true,
        'delete_others_ncpages'    => true,
        'delete_private_ncpages'   => true,
        'read_private_ncpages'     => true,

        // Allow to read and upload files
        'read' => true,  // access to Dashboard and user profile
        'upload_files' => true,  // see and add Media files
        'delete_posts' => true  // allows to delete media files they uploaded
    ));
}
// This makes it run only once at plugin activation
// __FILE__ is the full path to this php file.
register_activation_hook( __FILE__, 'fp_add_ncfpw_role' );

/********************************************************/
/*            Admin access to ncpage                    */
/********************************************************/

function fp_add_admin_caps() {
    $role = get_role( 'administrator' );
    $role -> add_cap( 'publish_ncpages' );
    $role -> add_cap( 'edit_ncpages' );
    $role -> add_cap( 'edit_others_ncpages' );
    $role -> add_cap( 'edit_private_ncpages' );
    $role -> add_cap( 'edit_published_ncpages' );
    $role -> add_cap( 'delete_ncpages' );
    $role -> add_cap( 'delete_published_ncpages' );
    $role -> add_cap( 'delete_others_ncpages' );
    $role -> add_cap( 'delete_private_ncpages' );
    $role -> add_cap( 'read_private_ncpages' );
}

add_action('admin_init', 'fp_add_admin_caps');
0
Jim Worrall 4 months 0 Answers 19 views 0

Leave an answer