BuddyPress: Different Registration Form For Different Types of Users

BuddyPress

Recently I worked for the first time with this WordPress plugin called BuddyPress that allows to create a social network like website.

It is a great tool and very flexible and in the version 2.2 they added the possibility to assign member types which is very useful to give different types of users different access.

One thing I couldn’t find how to do is to show different registration forms for different type of users. I had to show different set of fields for teachers and institutions. I managed to do that and I want to share the work around I came up with, with you.

Setting Up The Field Groups in BuddyPress

First of all I created a group for each type of user with the fields needed. After you install BuddyPress the option “Profile Fields” is added under Users:

BuddyPress - Profile Fields

Each of those field groups have an ID that you can see it if you position your mouse over the Edit Group button:

BuddyPress - Field Group ID

I’ll use the field group IDs later.

Modifying The Default Registration Form

In the home screen I added links to the registration form of each type of user. The links were the regular WordPress registration url plus the parameter role with the corresponding value.

<a href="exampleurl.com/register/?role=institution">Register as Institution</a>
<a href="exampleurl.com/register/?role=teacher">Register as Teacher</a>

In the registration file I added the follow code to save the validate the value of the parameter role and save it in a variable:

/* Save the value of the parameter role 
 * if is either teacher or institution, 
 * or set the variable value to FALSE otherwise */
$role =  (isset($_GET['role']) && ($_GET['role'] == 'teacher' || $_GET['role'] == 'institution')) ? $_GET['role'] : FALSE;

/* This is the Field Group ID corresponding 
 * to the user role, if it is an institution the group ID is 2
 * and if it's a teacher is the group 3 */
$profileField = $role ? (($role == 'institution') ? 2 : 3) : FALSE;

Then after the line 133 (version 2.3) where it says <?php if ( bp_is_active( 'xprofile' ) ) : ?> I added the following piece of code:

// check if the variable that has the ID of the group is not FALSE
if($profileField):
    // traditional BP loop through the fields in the group, in this case we use the value in the variable
    if ( bp_has_profile( array( 'profile_group_id' => $profileField, 'fetch_field_data' => false ) ) ) : 
	while ( bp_profile_groups() ) : bp_the_profile_group(); ?>

	<?php while ( bp_profile_fields() ) : bp_the_profile_field(); ?>

		<div<?php bp_field_css_class( 'form-group' ); ?>>

			<?php
			$field_type = bp_xprofile_create_field_type( bp_get_the_profile_field_type() );
                        // I used bootstrap so I needed the class "form-control" in the fields
			$field_type->edit_field_html(array('class'=>'form-control'));

			/**
			 * Fires before the display of the visibility options for xprofile fields.
			 *
			 * @since BuddyPress (1.7.0)
			 */
			do_action( 'bp_custom_profile_edit_fields_pre_visibility' );

			if ( bp_current_user_can( 'bp_xprofile_change_field_visibility' ) ) : ?>
				<p class="field-visibility-settings-toggle" id="field-visibility-settings-toggle-<?php bp_the_profile_field_id() ?>">
					<?php printf( __( 'This field can be seen by: <span class="current-visibility-level">%s</span>', 'buddypress' ), bp_get_the_profile_field_visibility_level_label() ) ?> <a href="#" class="visibility-toggle-link"><?php _ex( 'Change', 'Change profile field visibility level', 'buddypress' ); ?></a>
				</p>

				<div class="field-visibility-settings" id="field-visibility-settings-<?php bp_the_profile_field_id() ?>">
					<fieldset>
						<legend><?php _e( 'Who can see this field?', 'buddypress' ) ?></legend>

						<?php bp_profile_visibility_radio_buttons() ?>

					</fieldset>
					<a class="field-visibility-settings-close" href="#"><?php _e( 'Close', 'buddypress' ) ?></a>

				</div>
			<?php else : /*?>
				<p class="field-visibility-settings-notoggle" id="field-visibility-settings-toggle-<?php bp_the_profile_field_id() ?>">
					<?php printf( __( 'This field can be seen by: <span class="current-visibility-level">%s</span>', 'buddypress' ), bp_get_the_profile_field_visibility_level_label() ) ?>
				</p>
			<?php */ endif ?>

			<?php

			/**
			 * Fires after the display of the visibility options for xprofile fields.
			 *
			 * @since BuddyPress (1.1.0)
			 */
			do_action( 'bp_custom_profile_edit_fields' ); ?>

			<p class="description"><?php bp_the_profile_field_description(); ?></p>

		</div>

	<?php endwhile; ?>

	<input type="hidden" name="signup_profile_field_ids" id="signup_profile_field_ids" value="<?php bp_the_profile_field_ids(); ?>" />

	<?php endwhile; endif; 
endif ?>

What I did above was to loop through the different fields in the field group that I created before. If it was a teacher the corresponding field group ID is 3 and for institutions is 2, that number is saved in the variable $profileField.

I hope this helps you guys!