Updated BuddyPress User Roles Code

Posted by on Jan 27, 2011 in Articles, How-To | 14 comments

My post on adding user types to WordPress and Buddypress has been very popular.

This code will soon be available as a plugin.

I’ve updated the code so it’s cleaner and easier to implement. This code also works with or without allowing users to add new sites.

This only works for BuddyPress running on the multisite version of WordPress.
If you’re using the regular version, someone else has a plugin available for that: WP Roles at Registration. It works for BuddyPress (but not the multisite version).

Instructions

Set up your custom WordPress roles using Capability Manager or a similar plugin.
Place the following code into the functions.php file in your theme.
Edit the section titled Define User Roles and replace the values with your own.

<?php
/*------ Adding User Types to BuddyPress - http://cleverness.org ------*/

/**** == Define User Roles == ****/
/*
Replace usertypea with the name of the WordPress Role.
Replace User Type A with the label that you want to display on the registration form.
Repeat for the other roles.
Add or remove lines as needed, using this format:
'myrole' => 'My Role',
*/
 $customroles = array(
	'usertypea' => 'User Type A',
	'usertypeb' => 'User Type B',
	'usertypec' => 'User Type C',
	);
/**** == end Define User Roles == ****/

add_filter( 'bp_signup_usermeta', 'customroles_user_signup_field' );
add_filter( 'bp_core_activate_account', 'customroles_user_activate_field' );
add_action( 'wpmu_activate_user', 'customroles_register_role', 10, 3 );
add_action( 'wpmu_new_blog', 'customroles_register_role_blog', 10, 6 );
add_action( 'bp_after_account_details_fields', 'customroles_registration_fields' );

/* Add sign-up field to BuddyPress sign-up array */
function customroles_user_signup_field( $usermeta ) {
	$usermeta['signup_type'] = $_POST['signup_type'];
	return $usermeta;
}

/* Add sign-up field to usermeta on user activation */
function customroles_user_activate_field( $signup ) {
	update_usermeta( $signup['user_id'], 'signup_type', $signup['meta']['signup_type'] );
	return $signup;
}

/* Update the user's WP role and user meta */
function customroles_register_role($user_id, $password, $meta) {
	global $bp, $wpdb, $customroles;
   	$userdata = array();
   	$userdata['ID'] = $user_id;
   	$userdata['role'] = $meta['signup_type'];
	$type = $meta['signup_type'];

   	//only allow if user role is one of the allowed roles
   	if ( array_key_exists( $userdata['role'], $customroles ) ) {
      	wp_update_user($userdata);
		update_usermeta( $user_id, 'signup_type', $type );
   	}
}

/* Updates the user's WP role for when a new user creates a blog */
function customroles_register_role_blog($blog_id, $user_id, $domain, $path, $site_id, $meta) {
	global $bp, $wpdb, $customroles;
   	$userdata = array();
   	$userdata['ID'] = $user_id;
   	$userdata['role'] = $meta['signup_type'];
	$type = $meta['signup_type'];

   	//only allow if user role is one of the allowed roles
   	if ( array_key_exists( $userdata['role'], $customroles ) ) {
      	wp_update_user($userdata);
		update_usermeta( $user_id, 'signup_type', $type );
   	}
}

/* Add user type fields to registration form */
function customroles_registration_fields() {
	global $customroles;
	echo '
		<!-- Begin User Type code -->
		<div class="register-section" id="usertype-details-section">
		<div class="editfield">
			<label for="signup_type">Account Type:</label>
			<p><select id="signup_type" name="signup_type">';
			foreach ( $customroles as $key => $value ) {
				echo '<option value="'.$key.'">'.$value.'</option>';
				}
		echo'</select></p>
		</div></div>
	<!-- end of User Type code -->';
}
/*------ end Adding User Types to BuddyPress ------*/
?>

You can use get_user_meta() in your theme files to show different things to different user types.

<?php
global $bp;
if ( get_user_meta($bp->displayed_user->id, 'signup_type') == 'usertypea' ) : ?>
    Put your HTML code here
<?php endif; ?>

14 Comments

  1. Hey,

    its a very great solution. Thanks for that tutorial and plugin.
    But when a user tries to activate his account (from the link in the email), then an error comes:

    “Fatal error: Cannot use string offset as an array in … ”

    That is this part:
    /* Add sign-up field to usermeta on user activation */
    function customroles_user_activate_field( $signup ) {
    update_usermeta( $signup[‘user_id’], ‘signup_type’, $signup[‘meta’][‘signup_type’] );
    return $signup;
    }

    And the role is still subscriber, not the chosen custom role, even its defined with the WP plugin “Members”.
    Do you know, where the problem is?

    Sorry about my bad English.

    Best regards from Germany.
    Elec

    • I haven’t tested this in recent versions of BuddyPress, so it’s possible that the variable that is passed at bp_core_activate_account has changed. I haven’t kept up with BuddyPress development, so I’m not sure.

  2. I’m sorry, I meant has there been any further progress on the plugin. I’m glad somebody else thinks this is a needed feature for BP.

    • Not yet. I’ve been working on my To-Do List plugin lately. I have the plugin added to the WordPress plugin repository, but I haven’t gotten around to completing it yet. I’ll try to get to work on it once I finish this version of the To-Do List.

    • Any word on plugin progress?

  3. I have been looking for a solution such as this for quite a long time. Is the plugin available?

  4. OK I placed the code in the functions.php… created the roles and changed the code to apply to the roles…
    I do a sign up and all good… then I placed the global $bp;
    if ( get_user_meta($bp->displayed_user->id, ‘signup_type’) == ‘usertypea’ ) : ?>
    Put your HTML code here

    in the profile.php.. changed “usertypea” to the role name.. but the (Put your HTML code here) doesnt show …. Im guess the usertype is not bein save??

    • reading the code I see
      add_action( ‘wpmu_activate_user’, ‘customroles_register_role’, 10, 3 );
      add_action( ‘wpmu_new_blog’, ‘customroles_register_role_blog’, 10, 6 );

      Im running wordpress 3.1.1 would it be

      add_action( ‘wpms_activate_user’, ‘customroles_register_role’, 10, 3 );
      add_action( ‘wpms_new_blog’, ‘customroles_register_role_blog’, 10, 6 );

      ???

  5. I placed this code in my functions.php but it’s still adding people as “subscriber” rather than their new role/s I have created. I’m using the member’s plugin by Justin Tadlock. Any idea what could be happening? I’ve double checked my roles and also double checked that I changed the “Define User roles” section in your code above. I’m running the latest version of BuddyPress and WordPress

    • Err cancel that 🙂 Was conflicting with something else. I don’t suppose you could give me any ideas on how to filter the BuddyPress member’s loop based on their new WP role?

  6. how can we add different fields for different account types? example a venue user will add their address and hours… etc…

    Also Im getting some kind of error that when i try to logout it give me a blank page and doesnt log me out…

    Thank you!

  7. I have been looking for this for some time. I get an error when I tried your code. I use latest buddypress and WP 3.0.5. I used Cap to set up 2 new user roles and replaced the relevant code

    This is the error i get
    Parse error: syntax error, unexpected ‘<' in /…/functions.php on line 77

    • Is there any punctuation in your user type label? The error is occuring on the line that outputs the dropdown box on the registration page. As the code is written right now, any non-letter or number character will likely break it.

Trackbacks/Pingbacks

  1. Adding User Types to WordPress and BuddyPress – with User Blogs - [...] 13th, 2010 There is a new version of this code located at http://cleverness.org/2011/01/27/updated-buddypress-user-roles-code/. Please use that code and…
  2. Add User Types to WordPress and BuddyPress - [...] 8th, 2010 There is a new version of this code located at http://cleverness.org/2011/01/27/updated-buddypress-user-roles-code/. Please use that code and…

Leave a Reply to OnYx Cancel reply

Your email address will not be published. Required fields are marked *