BuddyPress: Assign Custom Roles To Users On Registration


In the recent post BuddyPress: Different Registration Form For Different Types of Users I explain how to show different fields for different types of users in BuddyPress.

The project that I talked about in that post also required different roles for the different types of users. Here is my work around for that.

Creating the roles

In the theme’s function.php file we add the function that creates the roles to have them ready when a new user registers.

function my_theme_setup(){
 // your code
 * Add Theme roles
 * See: https://codex.wordpress.org/Function_Reference/add_role#Example
 add_role( 'teacher', 'Teacher', array(
	 'read' => true, // true allows this capability
	 'edit_posts' => true,
	 'delete_posts' => true, // Use false to explicitly deny
	 'edit_published_posts' => true,
	 'upload_files' => true,
	 'delete_published_posts' => true
 ) );
 add_role( 'institution', 'Institution', array(
	 'read' => true, // true allows this capability
	 'edit_posts' => false,
	 'delete_posts' => false // Use false to explicitly deny
 ) );
add_action( 'after_setup_theme', 'my_theme_setup' );

In the code above the role teacher has more capabilities than the role institution. You can add as many capabilities as you think your role needs. For more information about how to add capabilities to roles go to https://codex.wordpress.org/Function_Reference/add_role.

The Registration Form

In the previous post I also explained how to modify the registration form in BuddyPress.
Using the same parameter in the URL role we add a hidden input with that value to pass it when the form is submitted.

<?php $role = isset($_GET['role']) ? $_GET['role'] : ''; ?>
<input type="hidden" name="user_role" value="<?= $role; ?>" />

Functions To Assign The Role On Registration

In the functions.php file now let’s add to two more functions to handle the post with the role value: one to save the role as user_meta when the user submits the registration form and another to finally set the user’s role to the value saved in the user meta when the user is activated.

 * Get the value of the key "user_role"
function bp_custom_registration_role($user_id) {
	//searches the user role in the post
	$role = isset($_POST['user_role'] ? $_POST['user_role'] : FALSE;
	if ($role){
		//make sure that the post value matches the available options
		if (($role == "teacher") || ($role == "institution"))
			//save the value as user meta
			add_user_meta( $user_id, 'user_role', $role);
// attach the function in the BuddyPress 'bp_core_signup_user' action.
add_action('bp_core_signup_user', 'bp_custom_registration_role',10 , 1);

 * When the user is activated, sets the role
 * with the value saved as user meta
function bp_custom_activation_role($user) {
	if (! empty( $user ) ) {
		if ( is_array( $user ) ) {
			$user_id = $user['user_id'];
		} else {
			$user_id = $user;
		if ( ! empty( $user_id ) ) {
			//store the value of the user meta 'user_role' in a variable
			$role = get_user_meta($user_id, 'user_role', TRUE);
			//make sure the value matches the available options
			if (($role == "institution") || ($role == "teacher"))
				//sets the role to our custom role
				wp_update_user(array( 'ID' => $user_id, 'role' => $role ));
//attach the function to the BuddyPress 'bp_core_activated_user' action
add_action('bp_core_activated_user', 'bp_custom_activation_role');

In these functions you can also add a error handler for example if the role parameter is empty or doesn’t match the available options.

I hope this helps you if you ever have this issue!