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; ?>