wordpress author.php,WordPress功能函数author_can()

author_can()是WordPress中用于检查文章作者是否拥有特定权限的函数。它接受文章ID或对象,功能名称以及可选参数,并返回布尔值。此函数在处理用户权限,如编辑文章或元数据时非常有用。例如,可以使用它来判断作者是否能发布文章。
摘要由CSDN通过智能技术生成

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 );

}

更新日志:

83cd2bd3487e57fde585b7fb55b6b7cb.png

用户贡献的笔记

(由Codex - 5年前贡献)

例子

if ( author_can( $post->ID, 'publish_posts' ) ) {

echo __( 'Yes they can publish posts!', 'textdomain' );

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值