返回介绍

is_user_member_of_blog()

发布于 2017-09-11 01:31:26 字数 3750 浏览 1054 评论 0 收藏 0

is_user_member_of_blog( int $user_id,  int $blog_id )

Find out whether a user is a member of a given blog.


description


参数

$user_id

(int) (Optional) The unique ID of the user. Defaults to the current user.

$blog_id

(int) (Optional) ID of the blog to check. Defaults to the current site.


返回值

(bool)


源代码

File: wp-includes/user.php

function is_user_member_of_blog( $user_id = 0, $blog_id = 0 ) {
	global $wpdb;

	$user_id = (int) $user_id;
	$blog_id = (int) $blog_id;

	if ( empty( $user_id ) ) {
		$user_id = get_current_user_id();
	}

	// Technically not needed, but does save calls to get_site and get_user_meta
	// in the event that the function is called when a user isn't logged in
	if ( empty( $user_id ) ) {
		return false;
	} else {
		$user = get_userdata( $user_id );
		if ( ! $user instanceof WP_User ) {
			return false;
		}
	}

	if ( ! is_multisite() ) {
		return true;
	}

	if ( empty( $blog_id ) ) {
		$blog_id = get_current_blog_id();
	}

	$blog = get_site( $blog_id );

	if ( ! $blog || ! isset( $blog->domain ) || $blog->archived || $blog->spam || $blog->deleted ) {
		return false;
	}

	$keys = get_user_meta( $user_id );
	if ( empty( $keys ) ) {
		return false;
	}

	// no underscore before capabilities in $base_capabilities_key
	$base_capabilities_key = $wpdb->base_prefix . 'capabilities';
	$site_capabilities_key = $wpdb->base_prefix . $blog_id . '_capabilities';

	if ( isset( $keys[ $base_capabilities_key ] ) && $blog_id == 1 ) {
		return true;
	}

	if ( isset( $keys[ $site_capabilities_key ] ) ) {
		return true;
	}

	return false;
}

更新日志

Versiondescription
MU 1.1Introduced.

相关函数

Uses

  • wp-includes/ms-blogs.php: get_site()
  • wp-includes/pluggable.php: get_userdata()
  • wp-includes/load.php: is_multisite()
  • wp-includes/load.php: get_current_blog_id()
  • wp-includes/user.php: get_user_meta()
  • wp-includes/user.php: get_current_user_id()
  • Show 1 more use Hide more uses

Used By

  • wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php: WP_REST_Users_Controller::get_user()
  • wp-includes/deprecated.php: is_blog_user()
  • wp-includes/class-wp.php: WP::handle_404()
  • wp-includes/admin-bar.php: wp_admin_bar_site_menu()
  • wp-includes/option.php: wp_user_settings()
  • wp-includes/option.php: wp_set_all_user_settings()
  • Show 1 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Check if user is member of a blog

    $user_id = get_current_user_id();
    $blog_id = 5;
    if ( is_user_member_of_blog( $user_id, $blog_id ) ) {
    	/*
        * The user is a member of the blog with ID 5.
        * We could do stuff here, like show them a link, etc.
        */
    }
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文