正在查看current_user_can()
\'s文档,我看到它使用WP_User::has_cap(). 因此,如果您的代码(或WP核心代码)使用current_user_can( \'edit_post\', $post->ID )
要确定当前用户是否可以编辑当前帖子,可以使用user_has_cap
过滤器(调入WP_User::has_cap()
):
add_filter( \'user_has_cap\', \'wpse360937_allow_manager\', 10, 4 );
function wpse360937_allow_manager( $allcaps, $caps, $args, $user ) {
// Bail out if we\'re not asking about a post:
if ( \'edit_post\' != $args[0] ) {
return $allcaps;
}
// Bail out for users who can already edit others posts:
if ( $allcaps[\'edit_others_posts\'] ) {
return $allcaps;
}
// Bail out if the user is the post author:
if ( $args[1] == $post->post_author ) {
return $allcaps;
}
$post_id = $args[2];
$manager_id = get_post_meta( $post_id, \'manager\', true );
// Assumes the meta field is called "manager"
// and contains the User ID of the manager.
if ( $manager_id == $user->ID ) {
$allcaps[ $caps[0] ] = true;
}
return $allcaps;
}
(代码部分基于
User Contributed Notes on the user_has_cap
filter docs.)