admin管理员组

文章数量:1126314

I am in the process of creating a form for a user to fill out to create a listing request which creates a CPT post and user record.

In this process, I want to copy the subscriber role, rename it 'username-role' and assign it to the new user.

I want to then allow the new role 'username-role' the edit_post capability on that one single CPT post they created.

Basically, I want people to come to the website, create a listing and then be able to log in and edit that listing (and only that listing)

How can I give a user role and capabilities object permission to edit one single CPT post only?

I am in the process of creating a form for a user to fill out to create a listing request which creates a CPT post and user record.

In this process, I want to copy the subscriber role, rename it 'username-role' and assign it to the new user.

I want to then allow the new role 'username-role' the edit_post capability on that one single CPT post they created.

Basically, I want people to come to the website, create a listing and then be able to log in and edit that listing (and only that listing)

How can I give a user role and capabilities object permission to edit one single CPT post only?

Share Improve this question asked Jan 19, 2024 at 7:09 php-b-graderphp-b-grader 4182 gold badges7 silver badges21 bronze badges 4
  • 1 None of this should be necessary. Are you assigning the user as the author of the post? If so, just give them a role that doesn't have the edit_others_posts capability. – Jacob Peattie Commented Jan 19, 2024 at 8:33
  • @JacobPeattie thanks. I did see that but all i kept thinking was "no, i don't want them to edit others posts"... ha – php-b-grader Commented Jan 20, 2024 at 10:23
  • @JacobPeattie, I registered the users now as a contributor instead of subscriber and it all works fine. I've had to hide posts and comments from dashboard but it works. Thanks. Q: Do I even need to create new role as a copy? What is the benefit of this? – php-b-grader Commented Jan 22, 2024 at 3:15
  • 1 There's no reason to copy the role. – Jacob Peattie Commented Jan 22, 2024 at 3:59
Add a comment  | 

1 Answer 1

Reset to default -2

Add code - your current active theme's functions.php file or in a custom plugin:

// Step 1: Create a New Role Based on Subscriber
function create_custom_role() {
    // Check if the role doesn't already exist
    if (!get_role('username-role')) {
        // Get the capabilities of the Subscriber role
        $subscriber_role = get_role('subscriber');
        $capabilities = $subscriber_role->capabilities;

        // Create a new role based on Subscriber
        add_role('username-role', 'Username Role', $capabilities);
    }
}
add_action('init', 'create_custom_role');

// Step 2: Assign the New Role to the User
function assign_custom_role_to_user($user_id) {
    $user = new WP_User($user_id);
    $user->set_role('username-role');
}
add_action('user_register', 'assign_custom_role_to_user');

// Step 3: Grant Edit Post Capability for the CPT
function grant_edit_post_capability($post_id, $post, $update) {
    // Check if it's the desired CPT post type
    if ($post->post_type === 'your_cpt') { 
        // Please replace 'your_cpt' with the actual slug of your custom post type

        // Get the user who created the post
        $user_id = $post->post_author;

        // Get the user's role
        $user = new WP_User($user_id);
        $user->add_cap('edit_post', $post_id);
    }
}
add_action('save_post', 'grant_edit_post_capability', 10, 3);

本文标签: Allowing a CPT post to be edited by a single user role