bbp_set_current_user_default_role

Add the default role to the current user if needed.

Description

bbp_set_current_user_default_role(); 

This function will bail if the forum is not global in a multisite installation of WordPress, or if the user is marked as spam or deleted.


Usage

  1. if ( !function_exists( 'bbp_set_current_user_default_role' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/users/capabilities.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = bbp_set_current_user_default_role(); 
  7.  

Defined (1)

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

/includes/users/capabilities.php  
  1. function bbp_set_current_user_default_role() { 
  2.  
  3. /** Sanity ****************************************************************/ 
  4.  
  5. // Bail if deactivating bbPress 
  6. return; 
  7.  
  8. // Catch all, to prevent premature user initialization 
  9. if ( ! did_action( 'set_current_user' ) ) 
  10. return; 
  11.  
  12. // Bail if not logged in or already a member of this site 
  13. if ( ! is_user_logged_in() ) 
  14. return; 
  15.  
  16. // Get the current user ID 
  17. $user_id = bbp_get_current_user_id(); 
  18.  
  19. // Bail if user already has a forums role 
  20. if ( bbp_get_user_role( $user_id ) ) 
  21. return; 
  22.  
  23. // Bail if user is marked as spam or is deleted 
  24. if ( bbp_is_user_inactive( $user_id ) ) 
  25. return; 
  26.  
  27. /** Ready *****************************************************************/ 
  28.  
  29. // Load up bbPress once 
  30. $bbp =bbpress); 
  31.  
  32. // Get whether or not to add a role to the user account 
  33. $add_to_site = bbp_allow_global_access(); 
  34.  
  35. // Get the current user's WordPress role. Set to empty string if none found. 
  36. $user_role = bbp_get_user_blog_role( $user_id ); 
  37.  
  38. // Get the role map 
  39. $role_map = bbp_get_user_role_map(); 
  40.  
  41. /** Forum Role ************************************************************/ 
  42.  
  43. // Use a mapped role 
  44. if ( isset( $role_map[$user_role] ) ) { 
  45. $new_role = $role_map[$user_role]; 
  46.  
  47. // Use the default role 
  48. } else { 
  49. $new_role = bbp_get_default_role(); 
  50.  
  51. /** Add or Map ************************************************************/ 
  52.  
  53. // Add the user to the site 
  54. if ( true === $add_to_site ) { 
  55.  
  56. // Make sure bbPress roles are added 
  57.  
  58. $bbp->current_user->add_role( $new_role ); 
  59.  
  60. // Don't add the user, but still give them the correct caps dynamically 
  61. } else {  
  62. $bbp->current_user->caps[$new_role] = true; 
  63. $bbp->current_user->get_role_caps();