0

Please don't mark this as already asked question as I have searched all the questions and none of them gives a clear answer.

So I have created a custom role through Members plugin and I want the user with custom role 'Society Manager'to edit the custom post type post only and dont let them create new one. Admin should still be allowed to create new posts.

I have seen others using css/html and Jquery to remove the Add New button but it's not a good choice. The problem is I dont see create_posts option in Members plugin. So can anyone please help me solve this issue in detail? This is my custom post type code.


              register_post_type('society' , array(
                'show_in_rest' => true,
                'capability_type' => 'society',
                   'map_meta_cap' => true,  
                'capabilities' => array(
                    'create_posts' => 'do_not_allow',
                ),
                'supports' => array('title','thumbnail','author'),
                'rewrite' => array('slug' => 'societies'),
                'has_archive' => true,
                'public' => true,
                'labels' => array(
                    'name' => 'Societies',
                    'add_new_item' => 'Add New Society',
                    'edit_item' => 'Edit Society',
                    'all_items' => 'All Societies',
                    'singular_name' => 'Society'
                ),
                'menu_icon' => 'dashicons-buddicons-groups'
           ));

You can see there is no option to deny create_posts in members plugin enter image description here

3
  • CPTs get their own capabilities, otherwise they inherit from post if you don't specify that they should get their own
    – Tom J Nowell
    Commented Oct 28, 2019 at 19:35
  • I tried using 'capabilities' => array( 'create_posts' => 'do_not_allow') but i could still create new posts. That's why i am confused
    – Johnx
    Commented Oct 28, 2019 at 19:38
  • I don't believe that's how that parameter works in register_post_type, or rather that's not what it means. You're telling it what capability it is, not what it isn't. I'm not sure it's possible to only edit and not create, editing vs adding in WP is very similar with very few differences between the two
    – Tom J Nowell
    Commented Oct 28, 2019 at 20:40

1 Answer 1

0

You cannot find the capability because it does not exist. In WP out of the box, the paradigm is not post creation, but post publishing. Users can be adjusted to edit and create drafts and pending posts, but not publish.

Setting users to only edit however is not possible at the roles and capabilities level, and implementing that would be a significant amount of work on WP Core itself. For example, this would break autosaves and revision history if it was possible as is.

Additionally, the internal code paths for update posts vs creating posts are near identical. The important part being wether a post ID is specified when wp_insert_post is called.

It might be possible that you can intercept and prevent the posts creation during on of the post save hooks, but this is likely to have unanticipated knock on effects, as well as the UI still implying post creation is possible.

To summarise:

  • This isn't possible with vanilla WP
  • WP itself is built under the assumption it isn't possible
  • Making it possible is a significant amount of work involving WP Core ( possibly months or years of work )
  • You could kludge your way to it with filters, but the results will be awful with lots of difficult non-trivial bugs
  • The REST API and XMLRPC could bypass those

I strongly discourage against further pursuing this path, advise that it will be expensive in the extreme, and that the chances of failure are incredibly high. Find an alternative.

2
  • Thankyou for the detailed answer. I have researched alot and i have come up with a few ideas like disabling add new button via jquery somehowe and than dont let user go on add new url and redirect it back. Do you think it will work?
    – Johnx
    Commented Oct 29, 2019 at 14:09
  • That's a stopgap but not a full solution
    – Tom J Nowell
    Commented Oct 29, 2019 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.