admin管理员组

文章数量:1122846

Im using the User Role Editor plugin to try to acheive this, but I've also tried PublishPress Capabilities and had the exact same problem.

I'd like it so that my contributor users work like so:

  • Their posts need to be reviewed / published by an admin
  • They can edit their own posts but not others
  • They can edit their posts after they've been approved / published
  • They're edited post will need to be reviewed / published again by an admin

I can get it so that the users have the ability to edit their posts after theyve been published, however now I just get the blue 'update' button, and the posts can be updated by a contributor without needing to be approved by an admin.

If I remove edit_published_posts then it means contributors cannot edit their posts at all once they've been published.

Is this possible?

Below is my functions.php

function custom_contributor_capabilities() {
    // Get the Contributor role
    $contributor = get_role('contributor');
    
    // Allow editing their own posts, including published ones
    $contributor->add_cap('edit_posts');
    $contributor->add_cap('edit_published_posts');
    $contributor->add_cap('delete_posts');
    $contributor->add_cap('upload_files');
    
    // Remove the ability to publish or delete others' posts
    $contributor->remove_cap('publish_posts');
    $contributor->remove_cap('delete_published_posts');
    
    // Allow editing of only their own posts, but not others
    $contributor->remove_cap('edit_others_posts');
}
add_action('init', 'custom_contributor_capabilities');

Im using the User Role Editor plugin to try to acheive this, but I've also tried PublishPress Capabilities and had the exact same problem.

I'd like it so that my contributor users work like so:

  • Their posts need to be reviewed / published by an admin
  • They can edit their own posts but not others
  • They can edit their posts after they've been approved / published
  • They're edited post will need to be reviewed / published again by an admin

I can get it so that the users have the ability to edit their posts after theyve been published, however now I just get the blue 'update' button, and the posts can be updated by a contributor without needing to be approved by an admin.

If I remove edit_published_posts then it means contributors cannot edit their posts at all once they've been published.

Is this possible?

Below is my functions.php

function custom_contributor_capabilities() {
    // Get the Contributor role
    $contributor = get_role('contributor');
    
    // Allow editing their own posts, including published ones
    $contributor->add_cap('edit_posts');
    $contributor->add_cap('edit_published_posts');
    $contributor->add_cap('delete_posts');
    $contributor->add_cap('upload_files');
    
    // Remove the ability to publish or delete others' posts
    $contributor->remove_cap('publish_posts');
    $contributor->remove_cap('delete_published_posts');
    
    // Allow editing of only their own posts, but not others
    $contributor->remove_cap('edit_others_posts');
}
add_action('init', 'custom_contributor_capabilities');
Share Improve this question asked Jun 16, 2024 at 18:18 user303096user303096 1032 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Steps for setting edited posts by contributors to "pending review":

1. Adjust Contributor Capabilities

Your modifications to the contributor role are appropriate for allowing them to edit their posts. Ensure they can edit posts but cannot publish them.

2. Automatically Revert Posts to "Pending Review" on Edit

Add the following code to your functions.php. This function sets a post to "pending review" when a contributor edits a previously published post:

function set_post_pending_review( $post_ID, $post_after, $post_before ) {
    if ( $post_before->post_status === 'publish' ) {
        $user = wp_get_current_user();
        if ( in_array( 'contributor', $user->roles ) ) {
            remove_action( 'post_updated', 'set_post_pending_review' ); // Avoid infinite loop
            wp_update_post( array(
                'ID'          => $post_ID,
                'post_status' => 'pending'
            ) );
            add_action( 'post_updated', 'set_post_pending_review', 10, 3 ); // Re-add the function
        }
    }
}
add_action( 'post_updated', 'set_post_pending_review', 10, 3 );

本文标签: