I am trying to limit a user role to only be able to edit existing pages, but not create new ones or do anything else like anything with posts.
I have read this:
Is there an existing capability to allow editing of only pre-existing pages? If not, a good way to implement this? and the ticket mentioned there: https://core.trac.wordpress.org/ticket/16714
So I created a new user role with those permissions:
edit_pages
edit_others_pages
edit_published_pages
read
Then I added this:
function disable_page_creation(){
if( check_user_role('rolename'){ // This checks if the current user belongs to this role
get_post_type_object('page')->cap->create_posts = 'do_not_allow';
}
}
The problem is that this not only disables creation of new pages but also makes anything else like listing pages forbidden.
As soon as I just add edit_posts
everything is working as expected in the pages section, but of course posts are editable now.
Is this something WP just isn't capable of in this particular combination or am I just doing it wrong? Any hints where it gets stuck?
EDIT: Okay I think I am tracing down the issue. I believe the reason this happens is that posts and pages share the same admin file edit.php
. Some more details:
The global $pagenow
variable is set to edit.php
no matter if we edit a post or a page.
In user_can_access_admin_page() this check is performed
if ( isset( $_wp_menu_nopriv[$pagenow] ) ){
As the disabled post-editing puts edit.php
in $_wp_menu_nopriv[$pagenow]
it also bails out for pages.
I'd say this is a WP bug. Can anyone confirm or does have a solution?