bbp_profile_update_role

Helper function hooked to 'bbp_profile_update' action to save or update user roles and capabilities.

Description

bbp_profile_update_role( (int) $user_id = 0 ); 

Parameters (1)

0. $user_id — Optional. (int)
The user id.

Usage

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

Defined (1)

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

/includes/users/capabilities.php  
  1. function bbp_profile_update_role( $user_id = 0 ) { 
  2.  
  3. // Bail if no user ID was passed 
  4. if ( empty( $user_id ) ) 
  5. return; 
  6.  
  7. // Bail if no role 
  8. if ( ! isset( $_POST['bbp-forums-role'] ) ) 
  9. return; 
  10.  
  11. // Fromus role we want the user to have 
  12. $new_role = sanitize_text_field( $_POST['bbp-forums-role'] ); 
  13. $forums_role = bbp_get_user_role( $user_id ); 
  14.  
  15. // Bail if no role change 
  16. if ( $new_role === $forums_role ) 
  17. return; 
  18.  
  19. // Bail if trying to set their own role 
  20. return; 
  21.  
  22. // Bail if current user cannot promote the passing user 
  23. if ( ! current_user_can( 'promote_user', $user_id ) ) 
  24. return; 
  25.  
  26. // Set the new forums role 
  27. bbp_set_user_role( $user_id, $new_role );