is_user_member_of_blog()WP 3.0.0

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

No Hooks.

Returns

true|false.

Usage

is_user_member_of_blog( $user_id, $blog_id );
$user_id(int)
The unique ID of the user.
Default: current user
$blog_id(int)
ID of the blog to check.
Default: current site

Notes

  • Global. wpdb. $wpdb WordPress database abstraction object.

Changelog

Since 3.0.0 Introduced.

is_user_member_of_blog() code WP 6.9.1

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

	if ( 1 === $blog_id ) {
		$capabilities_key = $wpdb->base_prefix . 'capabilities';
	} else {
		$capabilities_key = $wpdb->base_prefix . $blog_id . '_capabilities';
	}
	$has_cap = get_user_meta( $user_id, $capabilities_key, true );

	return is_array( $has_cap );
}