user_can_edit_post()
云策文档标注
概述
user_can_edit_post() 是一个已弃用的 WordPress 函数,用于检查指定用户是否可以编辑指定文章。自 WordPress 2.0.0 起,建议使用 current_user_can() 替代。
关键要点
- 函数已弃用:自 WordPress 2.0.0 起,应使用 current_user_can() 替代。
- 参数:接受 $user_id(必需)、$post_id(必需)和 $blog_id(可选,默认值为 1,但未使用)。
- 返回值:返回布尔值,表示用户是否有编辑权限。
- 权限逻辑:基于用户级别和文章作者身份进行判断,包括用户是否为文章作者、文章状态是否为发布,以及用户级别比较。
代码示例
function user_can_edit_post($user_id, $post_id, $blog_id = 1) {
_deprecated_function( __FUNCTION__, '2.0.0', 'current_user_can()' );
$author_data = get_userdata($user_id);
$post = get_post($post_id);
$post_author_data = get_userdata($post->post_author);
if ( (($user_id == $post_author_data->ID) && !($post->post_status == 'publish' && $author_data->user_level > $post_author_data->user_level))
|| ($author_data->user_level >= 10) ) {
return true;
} else {
return false;
}
}注意事项
- 此函数已弃用,新代码中应避免使用,改用 current_user_can() 进行权限检查。
- 函数内部调用了 get_userdata()、get_post() 和 _deprecated_function() 等辅助函数。
- 权限判断逻辑可能不适用于所有场景,建议使用更灵活的 current_user_can() 函数。
原文内容
Whether user can edit a post.
Description
See also
Parameters
$user_idintrequired$post_idintrequired$blog_idintoptional-
Not Used
Default:
1
Source
function user_can_edit_post($user_id, $post_id, $blog_id = 1) {
_deprecated_function( __FUNCTION__, '2.0.0', 'current_user_can()' );
$author_data = get_userdata($user_id);
$post = get_post($post_id);
$post_author_data = get_userdata($post->post_author);
if ( (($user_id == $post_author_data->ID) && !($post->post_status == 'publish' && $author_data->user_level < 2))
|| ($author_data->user_level > $post_author_data->user_level)
|| ($author_data->user_level >= 10) ) {
return true;
} else {
return false;
}
}
Changelog
| Version | Description |
|---|---|
| 2.0.0 | Deprecated. Use current_user_can() |
| 1.5.0 | Introduced. |