WordPress功能函数author_can()

WordPress功能函数author_can(),返回所提供文章的作者是否具有指定的能力。au

WordPress功能函数author_can()

WordPress功能函数author_can(),返回所提供文章的作者是否具有指定的能力。
用法:
author_can( int|WP_Post $post, string $capability, mixed $args )
描述
这个函数还接受一个对象的ID,以检查该功能是否是元功能。诸如edit_post和edit_user之类的元功能是map_meta_cap()函数用来映射用户或角色具有的原始功能的功能,如edit_posts和edit_others_posts。
使用示例:
author_can( $post, \’edit_posts\’ );
author_can( $post, \’edit_post\’, $post->ID );
author_can( $post, \’edit_post_meta\’, $post->ID, $meta_key );
参数:
$post
(int|WP_Post) (必需) Post ID或Post对象。
$capability
(string) (必需) 功能名称。
$args
(mixed) (可选) 其他参数,通常从对象ID开始。
返回:
(bool) 文章作者是否具有所赋予的能力。
来源
文件: wp-includes/capabilities.php
function author_can( $post, $capability, …$args ) {
$post = get_post( $post );
if ( ! $post ) {
return false;
}
$author = get_userdata( $post->post_author );
if ( ! $author ) {
return false;
}
return $author->has_cap( $capability, …$args );
}
更新日志:

用户贡献的笔记
(由Codex – 5年前贡献)
例子
if ( author_can( $post->ID, \’publish_posts\’ ) ) {
echo __( \’Yes they can publish posts!\’, \’textdomain\’ );
}

作者: liuzhihao

为您推荐

返回顶部