bbp_set_current_user_default_role()

Add the default role to the current user if needed

Description

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.

Return

(If) not multisite, not global, or user is deleted/spammed

Source

File: bp-forums/users/capabilities.php

function bbp_set_current_user_default_role() {

	/** Sanity ****************************************************************/

	// Bail if deactivating Forums
	if ( bbp_is_deactivation() )
		return;

	// Catch all, to prevent premature user initialization
	if ( ! did_action( 'set_current_user' ) )
		return;

	// Bail if not logged in or already a member of this site
	if ( ! is_user_logged_in() )
		return;

	// Get the current user ID
	$user_id = bbp_get_current_user_id();

	// Bail if user already has a forums role
	if ( bbp_get_user_role( $user_id ) )
		return;

	// Bail if user is marked as spam or is deleted
	if ( bbp_is_user_inactive( $user_id ) )
		return;

	/** Ready *****************************************************************/

	// Load up Forums once
	$bbp         = bbpress();

	// Get whether or not to add a role to the user account
	$add_to_site = bbp_allow_global_access();

	// Get the current user's WordPress role. Set to empty string if none found.
	$user_role   = bbp_get_user_blog_role( $user_id );

	// Get the role map
	$role_map    = bbp_get_user_role_map();

	/** Forum Role ************************************************************/

	// Use a mapped role
	if ( isset( $role_map[$user_role] ) ) {
		$new_role = $role_map[$user_role];

	// Use the default role
	} else {
		$new_role = bbp_get_default_role();
	}

	/** Add or Map ************************************************************/

	// Add the user to the site
	if ( true === $add_to_site ) {

		// Make sure Forums roles are added
		bbp_add_forums_roles();

		$bbp->current_user->add_role( $new_role );

	// Don't add the user, but still give them the correct caps dynamically
	} else {		
		$bbp->current_user->caps[$new_role] = true;
		$bbp->current_user->get_role_caps();
	}
}

Changelog

Changelog
Version Description
bbPress (r3380) Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.