User Types in BuddyPress – Assign xprofile Groups

Posted by on Oct 13, 2010 in Articles, How-To | 16 comments

As requested by Janis, here is the code I use to show only certain xprofile groups to each user type. In the default BuddyPress theme, profile fields without a value aren’t shown, so only the ones for that group will appear to the public.

You will need to replace a function call in your BuddyPress theme, in /members/single/profile/edit.php. It is at line 12 in mine.

Find

<?php bp_profile_group_tabs(); ?>

And replace it with this:

<?php my_bp_profile_group_tabs(); ?>

In /plugins/bp-custom.php place the following code. You will need to edit it to fit your user type and profile group needs.

function my_bp_profile_group_tabs() {
	global $bp, $group_name;

	$signup_type = get_user_meta($bp->loggedin_user->id, 'signup_type', true);

	if ( !$groups = wp_cache_get( 'xprofile_groups_inc_empty', 'bp' ) ) {
		$groups = BP_XProfile_Group::get( array( 'fetch_fields' => true ) );
		wp_cache_set( 'xprofile_groups_inc_empty', $groups, 'bp' );
	}

	if ( empty( $group_name ) )
		$group_name = bp_profile_group_name(false);

	for ( $i = 0; $i < count($groups); $i++ ) {
	if ( $signup_type == 'usertypea' && ( $groups[$i]->name == 'Base Profile' || $groups[$i]->name == 'Profile Group A' || $groups[$i]->name == 'Profile Group B' ) ) {
		if ( $group_name == $groups[$i]->name ) {
			$selected = ' class="current"';
		} else {
			$selected = '';
		}

		if ( $groups[$i]->fields )
			echo '<li' . $selected . '><a href="' . $bp->displayed_user->domain . $bp->profile->slug . '/edit/group/' . $groups[$i]->id . '">' . attribute_escape( $groups[$i]->name ) . '</a></li>';

	} elseif ( $signup_type == 'usertypeb' && ( $groups[$i]->name == 'Base Profile' || $groups[$i]->name == 'Profile Group C' ) ) {
		if ( $group_name == $groups[$i]->name ) {
			$selected = ' class="current"';
		} else {
			$selected = '';
		}

		if ( $groups[$i]->fields )
			echo '<li' . $selected . '><a href="' . $bp->displayed_user->domain . $bp->profile->slug . '/edit/group/' . $groups[$i]->id . '">' . attribute_escape( $groups[$i]->name ) . '</a></li>';
	} elseif ( $signup_type == 'usertypec' && ( $groups[$i]->name == 'Base Profile' || $groups[$i]->name == 'Profile Group D'  ) ) {
		if ( $group_name == $groups[$i]->name ) {
			$selected = ' class="current"';
		} else {
			$selected = '';
		}

		if ( $groups[$i]->fields )
			echo '<li' . $selected . '><a href="' . $bp->displayed_user->domain . $bp->profile->slug . '/edit/group/' . $groups[$i]->id . '">' . attribute_escape( $groups[$i]->name ) . '</a></li>';
		}
	}

	do_action( 'xprofile_profile_group_tabs' );
}

16 Comments

  1. It would be much better if BP group can category by TYPES.

    I know the buddypress 2.7 support to group types, but need develop plugins to make it work, any plan to develop one?

    Alex

  2. Hi Cindy, I have been stuck a couple months on a project of mine and was hoping you could help me in the right direction.

    I have two different users on my buddypress site teachers and students and I would like to different registration form for my two different user types.

    The ultimate solution would be to have a radio button or dropdown menu and when the user chooses their selection relevant to them (for example student) xprofile field group specefic for them would show up below.

    I’am a beginner at this I had some problems inserting the code, any advice or help you could give I would really appreciate it.

    Thank you for your time,

    Sincerely, Boris

    • Unfortunately I don’t really have any advice. It’s been about 2 years since I’ve worked with BuddyPress. The buddypress.org support forums may be able to help you though, if you haven’t tried there yet.

  3. Hey guys

    I have added everything to my bp-custom.php file, and i’ve also adjusted my edit.php file as noted above. What do I do now?

    I need to have different profile types…such as Models, Actors, Musicians and so on. During registration, the user needs to be given the option of selecting a profile type. I’m using the WP User Roles at Registration plugin for this. I think it works? If not…I can just deactivate and delete it. Once they select a profile type, they need to be able to add their appropriate details, example: if they selected Model, they need to input height, weight, hair color and so on. If they selected musician, they need to input band name, record label and so on.

    NOTE: This process does not have to happen during registration. I am okay with the user simply selecting a profile type, then coming back after account activation to edit their profile details. Either way…different profile types need to display different profile fields, and the profile account holders should be able to edit these custom fields from theit Profile > Edit screen.

    Thank you so much for the help in advance 🙂

    Ryan

    PS – the website I’m trying to do this on is at:
    theworldwaitingtv [dot] com

    if you need a login to get in and check things out, please let me know…I’ll gladly send you a login.

    • It is likely that the WP User Roles at Registration doesn’t create the user meta field signup_type. You would need to set the $signup_type variable to whatever that plugin creates, or add code to get the user’s role and set that to $signup_type.

      It’s also possible that some of BuddyPress’s code has changed. This code is two years old and I haven’t kept up with BuddyPress development so I don’t know if they’ve changed any functions.

  4. Hi Cindy

    Thanks for the sample code! I adapted it to use Membership levels from the Membership plugin, and it works like a charm.

    Cheers,

    Clare

    • Hi Clare, as you did?

      • Hi Joysan

        I’m not sure what you mean, but here’s what I did:

        As Cindy describes above, I changed the name of the bp_profile_group_tabs(); function. I’m using StudioPress templates, so mine was in plugins/genesis-connect/templates/members/single/profile/edit.php

        Then in my personal plugin (or I could have put it bp-custom.php), all I had to do was change the conditions Cindy checks for in lines 15, 25, and 34 above. I have several Membership levels. Most of them use several xprofile groups; one uses only the base groups, so that’s the only Membership level I had to check for in the function. My function is the same as Cindy’s until line 15:

        for ( $i = 0; $i name ) {
        $selected = ‘ class=”current”‘;
        } else {
        $selected = ”;
        }

        if ( $groups[$i]->fields ){
        echo ‘displayed_user->domain . $bp->profile->slug . ‘/edit/group/’ . $groups[$i]->id . ‘”>’ . attribute_escape( $groups[$i]->name ) . ‘‘;
        }

        }#else, user is buyer, do nothing, skip tab code so only base profile displays.
        }
        do_action( ‘xprofile_profile_group_tabs’ );

        I hope that answers your question!

        -clare

        • well, pasting that code didn’t work at all. half the lines are missing.

          basically, to check Membership level you just need to change the “if” statements in lines 15,25,and 34 to check the level id of the current user, using the ‘current_user_on_level’ function as described here:

          http://premium.wpmudev.org/forums/topic/conditionals-for-member-levels

          example: if ( current_user_on_level(‘3’) ) {

          • To put code in a comment, you can surround it with [ php ] [ / php ] (with no spaces between the words and the brackets). Thanks for taking the time to comment on how you did it.

          • Thanks for the formatting help Cindy, good to know:)

    • I’m glad you found it useful 🙂

  5. Hi cindy thank you for this articles, But I have a question
    When a user want to register a new account , is necessary to change the form in order to the Account type selected , what I have to do now ? I have followed all your suggestions in this article and
    in Updated BuddyPress User Roles Code also in this article you mentioned the function get_user_meta() “You can use get_user_meta() in your theme files to show different things to different user types.”

    in which file?
    should i create a different page_templare for each profile and registration page?

  6. Cindy, if my idea re “below line 29 in point 2” is wrong, no need to write additional codes, it is more than enough.
    If I will leave roles selection at signup page, will I still have possibility to change role of particular user in future (using Capability Manager Plugin)?

    thank you!

    • I’m glad to be of help.

      Yes, delete everything below line 29, until the closing php tag.

      It will assign the role to be a WordPress role, so you can do whatever you could normally with a role (in short: yes).

  7. hi Cindy,

    thank you so so so much, did not think that you will reply. I’ve been searching for this feature for weeks, even tried to write smth myself (have no skills).

    could you please help a bit.
    am I correct, that at the moment full structure is (?):

    1. Install Capability Manager Plugin;
    2. Create account types
    http://cleverness.org/2010/08/08/add-user-types-to-wordpress-and-buddypress/
    3. Assign xprofile Groups
    http://cleverness.org/2010/10/13/user-types-in-buddypress-assign-xprofile-groups/
    4. Allow user blogs
    http://cleverness.org/2010/10/13/adding-user-types-to-wordpress-and-buddypress-with-user-blogs/

    Roles are too hard for me, what if I want to leave account types + assign xprofile groups + allow blogs (roles will leave for future)…should I delete everything what goes below line 29 in point 2 ?

    thanks again!!

Leave a Reply

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