groups_is_user_member

Check whether a user is a member of a given group.

Description

(int|bool) groups_is_user_member( (int) $user_id, (int) $group_id ); 

Returns (int|bool)

ID of the membership if the user is member, otherwise false.

Parameters (2)

0. $user_id (int)
ID of the user.
1. $group_id (int)
ID of the group.

Usage

  1. if ( !function_exists( 'groups_is_user_member' ) ) { 
  2. require_once '/bp-groups/bp-groups-functions.php'; 
  3.  
  4. // ID of the user. 
  5. $user_id = -1; 
  6.  
  7. // ID of the group. 
  8. $group_id = -1; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = groups_is_user_member($user_id, $group_id); 
  12.  

Defined (1)

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

/bp-groups/bp-groups-functions.php  
  1. function groups_is_user_member( $user_id, $group_id ) { 
  2. $is_member = false; 
  3.  
  4. $user_groups = bp_get_user_groups( $user_id, array( 
  5. 'is_admin' => null,  
  6. 'is_mod' => null,  
  7. ) ); 
  8.  
  9. if ( isset( $user_groups[ $group_id ] ) ) { 
  10. $is_member = $user_groups[ $group_id ]->id; 
  11.  
  12. return $is_member;