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.
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.
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');
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.
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.
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');
Share
Improve this question
asked Sep 28, 2020 at 19:49
Jim WorrallJim Worrall
1777 bronze badges
4
- The layout section under the document tab isn't a part of WordPress but a Generatepress feature, you'll need to refer to their docs and support for help with that. as for authors, I see your CPT supports them, and you say the author dropdown works but these users with the unique role are not listed right? hmmm I wonder what's causing this – Tom J Nowell ♦ Commented Sep 28, 2020 at 20:22
- Thanks, Tom, that's already helpful. Yes the author dropdown works in Quick Edit, but the author with the custom role isn't listed. The author section in the editing Document tab is completely absent. Yes, hmmm! – Jim Worrall Commented Sep 28, 2020 at 20:39
- I know this may sound like a very rudimentary suggestion, but have you had them check the screen options at the top of the page (the tab in the upper right)? There is a checkbox to show/hide the "Author" dropdown, it generally remembers the setting based on user login so it may be enabled for you, but it could be that all they need to do is look and maybe check that box. – Trisha Commented Sep 29, 2020 at 19:24
- Thanks Trisha. I don't have that Screen Options control in the editing window (Wordpress 5.5.1). Maybe the author box doesn't show there if WP thinks there is only one author. It does show in the QuickEdit, but I (admin) am the only one in it. – Jim Worrall Commented Sep 30, 2020 at 15:31
1 Answer
Reset to default 0Answer for the missing Layout controls in the editor, after following up on Tom J Nowell's great tip that it is a GeneratePress feature. Tom at GeneratePress helped my solve it with a filter. By default, the Layout box is only shown for those with 'edit_theme_options' capability. The filter changes it to the custom capability, 'edit_ncpages'.
// Allow Layout metabox to be generated in editor.
// Otherwise only users with 'edit-theme-options' capability see it.
add_filter( 'generate_metabox_capability', function() {
return 'edit_ncpages';
} );
Or it could be just the 'read' capability, so anyone who gets into the editor has it.
Still haven't solved the other part - nobody with just the custom role shows up in the author dropdowns, although they can author and edit the custom post type.