bbp_set_user_role

Return a user's main role.

Description

(string) bbp_set_user_role( (int) $user_id = 0, (string) $new_role = '' ); 

Returns (string)

Parameters (2)

0. $user_id — Optional. (int)
The user id.
1. $new_role — Optional. (string) => ''
The new role.

Usage

  1. if ( !function_exists( 'bbp_set_user_role' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/users/capabilities.php'; 
  3.  
  4. // The user id. 
  5. $user_id = -1; 
  6.  
  7. // The new role. 
  8. $new_role = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bbp_set_user_role($user_id, $new_role); 
  12.  

Defined (1)

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

/includes/users/capabilities.php  
  1. function bbp_set_user_role( $user_id = 0, $new_role = '' ) { 
  2.  
  3. // Validate user id 
  4. $user_id = bbp_get_user_id( $user_id, false, false ); 
  5. $user = get_userdata( $user_id ); 
  6.  
  7. // User exists 
  8. if ( !empty( $user ) ) { 
  9.  
  10. // Get users forum role 
  11. $role = bbp_get_user_role( $user_id ); 
  12.  
  13. // User already has this role so no new role is set 
  14. if ( $new_role === $role ) { 
  15. $new_role = false; 
  16.  
  17. // Users role is different than the new role 
  18. } else { 
  19.  
  20. // Remove the old role 
  21. if ( ! empty( $role ) ) { 
  22. $user->remove_role( $role ); 
  23.  
  24. // Add the new role 
  25. if ( !empty( $new_role ) ) { 
  26.  
  27. // Make sure bbPress roles are added 
  28.  
  29. $user->add_role( $new_role ); 
  30.  
  31. // User does don exist so return false 
  32. } else { 
  33. $new_role = false; 
  34.  
  35. return apply_filters( 'bbp_set_user_role', $new_role, $user_id, $user );