current_user_can_for_blog()
current_user_can_for_blog( int $blog_id, string $capabi…
current_user_can_for_blog( int $blog_id, string $capability, mixed $args )
返回当前用户是否具有给定站点的指定功能。
Returns whether the current user has the specified capability for a given site.
目录锚点:#说明#参数#返回#源码
说明(Description)
此函数还接受一个对象的ID,以检查该功能是否为元功能。元功能(如edit_post和edit_user)是map_Meta_cap()函数用来映射到用户或角色具有的基本功能(如edit_posts和edit_others_posts)的功能。
示例用法:
当前用户可以为博客编辑文章;
当前用户可以登录博客($blog_id,’edit_post’,$post->id);
当前用户可以登录博客($blog_id,’edit_post_meta’,$post->id,$meta_key);
参数(Parameters)
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| $blog_id | (int) | 必需 | 站点ID。 |
| $capability | (string) | 必需 | 能力名称。 |
| $args | (mixed) | 可选 | 其他参数,通常以对象ID开头。 |
返回(Return)
(bool)用户是否具有给定的能力。
源码(Source)
/**
* Whether current user has a capability or role for a given blog.
*
* @since 3.0.0
*
* @param int $blog_id Blog ID
* @param string $capability Capability or role name.
* @return bool
*/
function current_user_can_for_blog( $blog_id, $capability ) {
$switched = is_multisite() ? switch_to_blog( $blog_id ) : false;
$current_user = wp_get_current_user();
if ( empty( $current_user ) ) {
if ( $switched ) {
restore_current_blog();
}
return false;
}
$args = array_slice( func_get_args(), 2 );
$args = array_merge( array( $capability ), $args );
$can = call_user_func_array( array( $current_user, 'has_cap' ), $args );
if ( $switched ) {
restore_current_blog();
}
return $can;
}| 更新版本 | 源码位置 | 使用 | 被使用 |
|---|---|---|---|
| 5.3.0 | wp-includes/capabilities.php:691 | 0 | 4 |
类别:WordPress 函数手册、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

还没有任何评论,赶紧来占个楼吧!