user_can

函式
user_can ( $user, $capability, $args )
引數
  • (int|WP_User) $user User ID or object.
    Required:
  • (string) $capability Capability name.
    Required:
  • (mixed) $args Optional further parameters, typically starting with an object ID.
    Required:
返回值
  • (bool) Whether the user has the given capability.
定義位置
相關方法
current_user_canauthor_canis_user_adminuser_can_richedituser_admin_url
引入
3.1.0
棄用
-

user_can: 這個函式檢查一個使用者是否有特定的許可權。

返回一個特定的使用者是否擁有指定的許可權。

這個函式也接受一個物件的ID,以檢查該許可權是否是元許可權。元許可權,如`edit_post`和`edit_user`是由`map_meta_cap()`函式用來對映到一個使用者或角色擁有的原始許可權,如`edit_posts`和`edit_others_posts`。

使用例項:
user_can( $user->ID, ‘edit_posts’ );
user_can( $user->ID, ‘edit_post’, $post->ID );
user_can( $user->ID, ‘edit_post_meta’, $post->ID, $meta_key );

function user_can( $user, $capability, ...$args ) {
	if ( ! is_object( $user ) ) {
		$user = get_userdata( $user );
	}

	if ( empty( $user ) ) {
		// User is logged out, create anonymous user object.
		$user = new WP_User( 0 );
		$user->init( new stdClass );
	}

	return $user->has_cap( $capability, ...$args );
}

常見問題

FAQs
檢視更多 >