current_user_can_for_blog()判断当前用户角色对指定博客是否有权限(仅适用于多站点)

目录

描述

判断当前用 角色对指 博客是否有权限。这 函数仅适用于多 点multisite。

This function is similar to current_user_can(), but allows you to check if the current user has a capability for another blog on the network. To check if the user has the capability for the current blog, you can just use current_user_can().

current_user_can_for_blog() 描述

用法

<?php current_user_can_for_blog($blog_id$capability); ?>

current_user_can_for_blog() 用法

参数

$blog_id

(integer) (必填) Blog ID

默认值: None

$capability

(string) (必填) Capability or role name.

默认值: None

current_user_can_for_blog() 参数

返回值

(boolean

current_user_can_for_blog() 返回值

历史

添加于 版本: 3.0

current_user_can_for_blog() 历史

源文件

current_user_can_for_blog() 函数的代码位于 wp-includes/capabilities.php

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * 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;
}

current_user_can_for_blog() 源文件