您可以使用user_has_cap
筛选以动态检查和授予功能。
add_filter(\'user_has_cap\', \'contributor_can_edit_published_posts\', 10, 4);
function contributor_can_edit_published_posts($allcaps, $caps, $args, $user) {
global $post;
// Do we have a post?
if ( ! $post ) {
return $allcaps;
}
// Is the user a contributor?
if ( ! isset( $allcaps[\'contributor\'] ) || true !== $allcaps[\'contributor\'] ) {
return $allcaps;
}
// Is the user the author of the post
if ( $post->post_author != $user->ID ) {
return $allcaps;
}
// Is the contributor allowed to edit this post?
if ( ! in_array( $post->ID, array(1,2,3,4,5) ) ) {
return $allcaps;
}
// Can the user edit published posts already? Allow, if not
if ( ! isset( $allcaps[\'edit_published_posts\'] ) && true !== $allcaps[\'edit_published_posts\'] ) {
$allcaps[\'edit_published_posts\'] = true;
}
return $allcaps;
}