is_user_member_of_blog

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

Description

is_user_member_of_blog( (int) $user_id = 0, (int) $blog_id = 0 ); 

Parameters (2)

0. $user_id — Optional. (int)
The unique ID of the user. Defaults to the current user.
1. $blog_id — Optional. (int)
ID of the blog to check. Defaults to the current site.

Usage

  1. if ( !function_exists( 'is_user_member_of_blog' ) ) { 
  2. require_once ABSPATH . WPINC . '/user.php'; 
  3.  
  4. // Optional. The unique ID of the user. Defaults to the current user. 
  5. $user_id = -1; 
  6.  
  7. // Optional. ID of the blog to check. Defaults to the current site. 
  8. $blog_id = -1; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = is_user_member_of_blog($user_id, $blog_id); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/user.php  
  1. function is_user_member_of_blog( $user_id = 0, $blog_id = 0 ) { 
  2. global $wpdb; 
  3.  
  4. $user_id = (int) $user_id; 
  5. $blog_id = (int) $blog_id; 
  6.  
  7. if ( empty( $user_id ) ) { 
  8. $user_id = get_current_user_id(); 
  9.  
  10. // Technically not needed, but does save calls to get_site and get_user_meta 
  11. // in the event that the function is called when a user isn't logged in 
  12. if ( empty( $user_id ) ) { 
  13. return false; 
  14. } else { 
  15. $user = get_userdata( $user_id ); 
  16. if ( ! $user instanceof WP_User ) { 
  17. return false; 
  18.  
  19. if ( ! is_multisite() ) { 
  20. return true; 
  21.  
  22. if ( empty( $blog_id ) ) { 
  23. $blog_id = get_current_blog_id(); 
  24.  
  25. $blog = get_site( $blog_id ); 
  26.  
  27. if ( ! $blog || ! isset( $blog->domain ) || $blog->archived || $blog->spam || $blog->deleted ) { 
  28. return false; 
  29.  
  30. $keys = get_user_meta( $user_id ); 
  31. if ( empty( $keys ) ) { 
  32. return false; 
  33.  
  34. // no underscore before capabilities in $base_capabilities_key 
  35. $base_capabilities_key = $wpdb->base_prefix . 'capabilities'; 
  36. $site_capabilities_key = $wpdb->base_prefix . $blog_id . '_capabilities'; 
  37.  
  38. if ( isset( $keys[ $base_capabilities_key ] ) && $blog_id == 1 ) { 
  39. return true; 
  40.  
  41. if ( isset( $keys[ $site_capabilities_key ] ) ) { 
  42. return true; 
  43.  
  44. return false;