Skip to:
Content
Pages
Categories
Search
Top
Bottom

2.2 Member Types – Setting user member types during registration (xProfile)

  • @tstrickland415

    Participant

    Hey all,

    The new BP 2.2 Member Types were really easy to implement, but is there a way to have a user select which member type they would like to be during registration and have it automatically reflect in the Dashboard (versus having to manually change each new user registration)?

    For example, I have two Member Types: Photographers and Video Directors. Can I link an xProfile field from the Registration page to automatically reflect which option they choose?

Viewing 25 replies - 1 through 25 (of 28 total)
  • @kobrakai75

    Participant

    That’s exactly what I’m trying to achieve. With the new Member Types in BP 2.2 it would so useful if members could select the type during registration, hopefully there’s a way!

    @bphelp

    Participant

    @tstrickland415 @kobrakai75
    I am not sure this is possible at this time but maybe someone else can chime in because I may be wrong because member types is still fairly new to BP and I have yet to play around with it.

    @dr_scythe

    Participant

    I’m thinking this might be possible using the gravity forms user registration addon and a member type field on the signup form.

    Gravity Forms user registration has a hook upon user creation so we could do something like:

    
    add_action("gform_user_registered", "add_custom_user_meta", 10, 4);
    function add_custom_user_meta($user_id, $config, $entry, $user_pass) {    
        $membertype = rgar( $entry, '1' );
        $set_member_type = bp_set_member_type( $user_id, $membertype );
    }
    

    The ‘1’ would be substituted for the field_id of the field where the user selects their member type (field value should be equal to the slug of the member type)

    @maelga

    Participant

    Has anyone managed to assign a member type at registration based on xprofile field?
    I do not use Gravity Forms…

    @danbp

    Participant

    Yes indeed, there is a way.

    Step by step tutorial with example code which you add to bp-custom.
    Note: use this code as is and follow the instruction. Once you understood what it does and how it works, you will be able to modify it to your needs.

    First, create a new xprofile field in the first group field (aka Base Group and containing Name as default field). Only fields created in that group are visible on the register page. Call it Type, enter description and choose multiselectbox as field type. Add operator, vendor, coach as select option.

    That’s all for the register part handled by BuddyPress.

    Now we need to declare the whole thing to get it work on front-end.
    Member-type is an additionnal functiony to select members by… type ! Not by role, not by latest, mst popular or anything else. Just (for the moment) by a custom type of your choice.

    We have to built a member directory for our types. We have 3 types and they will be shown on 3 new tabs on that directory.

    1) we formally declare the member types
    2) we count members by type (to stay correctly informative on the directory)
    3) we display the tabs

    function using_mt_register_member_types() {
    	bp_register_member_type( 'operator', array(
    		'labels' => array(
    			'name'          => __( 'Operators', 'using-mt' ),
    			'singular_name' => __( 'Operator', 'using-mt' ),
    		),
    	) );
    
    	bp_register_member_type( 'vendor', array(
    		'labels' => array(
    			'name'          => __( 'Vendors', 'using-mt' ),
    			'singular_name' => __( 'Vendor', 'using-mt' ),
    		),
    	) );
    
    	bp_register_member_type( 'coach', array(
    		'labels' => array(
    			'name'          => __( 'Coaches', 'using-mt' ),
    			'singular_name' => __( 'Coach', 'using-mt' ),
    		),
    	) );
    }
    add_action( 'bp_init', 'using_mt_register_member_types' );
    
    function using_mt_count_member_types( $member_type = '', $taxonomy = 'bp_member_type' ) {
    	global $wpdb;
    	$member_types = bp_get_member_types();
    
    	if ( empty( $member_type ) || empty( $member_types[ $member_type ] ) ) {
    		return false;
    	}
    
    	$count_types = wp_cache_get( 'using_mt_count_member_types', 'using_mt_bp_member_type' );
    
    	if ( ! $count_types ) {
    		if ( ! bp_is_root_blog() ) {
    			switch_to_blog( bp_get_root_blog_id() );
    		}
    
    		$sql = array(
    			'select' => "SELECT t.slug, tt.count FROM {$wpdb->term_taxonomy} tt LEFT JOIN {$wpdb->terms} t",
    			'on'     => 'ON tt.term_id = t.term_id',
    			'where'  => $wpdb->prepare( 'WHERE tt.taxonomy = %s', $taxonomy ),
    		);
    
    		$count_types = $wpdb->get_results( join( ' ', $sql ) );
    		wp_cache_set( 'using_mt_count_member_types', $count_types, 'using_mt_bp_member_type' );
    
    		restore_current_blog();
    	}
    
    	$type_count = wp_filter_object_list( $count_types, array( 'slug' => $member_type ), 'and', 'count' );
    	$type_count = array_values( $type_count );
    
    	if ( empty( $type_count ) ) {
    		return 0;
    	}
    
    	return (int) $type_count[0];
    }
    
    function using_mt_display_directory_tabs() {
    	$member_types = bp_get_member_types( array(), 'objects' );
    
    	// Loop in member types to build the tabs
    	foreach ( $member_types as $member_type ) : ?>
    
    	<li id="members-<?php echo esc_attr( $member_type->name ) ;?>">
    		<a href="<?php bp_members_directory_permalink(); ?>"><?php printf( '%s <span>%d</span>', $member_type->labels['name'], using_mt_count_member_types( $member_type->name ) ); ?></a>
    	</li>
    
    	<?php endforeach;
    }
    add_action( 'bp_members_directory_member_types', 'using_mt_display_directory_tabs' );

    We also need to sort the members list on each type tab using the loop scope.

    function using_mt_set_has_members_type_arg( $args = array() ) {
    	// Get member types to check scope
    	$member_types = bp_get_member_types();
    
    	// Set the member type arg if scope match one of the registered member type
    	if ( ! empty( $args['scope'] ) && ! empty( $member_types[ $args['scope'] ] ) ) {
    		$args['member_type'] = $args['scope'];
    	}
    
    	return $args;
    }
    add_filter( 'bp_before_has_members_parse_args', 'using_mt_set_has_members_type_arg', 10, 1 );

    And we finally clean the cache to stay up to date with the output

    function using_mt_clean_count_cache( $term = 0, $taxonomy = null ) {
    	if ( empty( $term ) || empty( $taxonomy->name ) || 'bp_member_type' != $taxonomy->name )  {
    		return;
    	}
    
    	wp_cache_delete( 'using_mt_count_member_types', 'using_mt_bp_member_type' );
    }
    add_action( 'edited_term_taxonomy', 'using_mt_clean_count_cache', 10, 2 );

    That’s all for a members directory page showing All Members and tabed Members by type.

    If you want to show the type of a member on his profile header, use this:

    function using_mt_member_header_display() {
    	$member_type = bp_get_member_type( bp_displayed_user_id() );
    
    	if ( empty( $member_type ) ) {
    		return;
    	}
    
    	$member_type_object = bp_get_member_type_object( $member_type );
    	?>
    	<p class="member_type"><?php echo esc_html( $member_type_object->labels['singular_name'] ); ?></p>
    
    	<?php
    }
    add_action( 'bp_before_member_header_meta', 'using_mt_member_header_display' );

    Or if you want to display the type under each user avatar on the member directory, you can use this snippet. Note: was originally made to add a geoloc shortcode below the member type. I let it as is, so you can see how it’s done.

    function who_are_you_directory() { // by member_type name + geoloc (wpgeo me)
    $user = bp_get_member_user_id();
    $terms = bp_get_object_terms( $user,  'bp_member_type' );
    
    	if ( ! empty( $terms ) ) {
    		if ( ! is_wp_error( $terms ) ) {		
    				foreach( $terms as $term ) {
    					echo '<p>' . $term->name . '</p>'; 					
    					echo do_shortcode('[gmw_member_info]');
    				}		
    		}
    	} 
    
    }
    add_filter ( 'bp_directory_members_item', 'who_are_you_directory' );

    Anything inspired by Codex and heavy topics reading.

    Member Types

    Members Loop

    May this help.

    @maelga

    Participant

    Thank you very much for this.

    I have just created 2 new members of different types.
    Interestingly the members are visible in the backend (under Users), but do not show up in members directory under neither of the tabs.
    The 3 tabs (one for each type) are displayed but all show 0 count.
    What makes me more perplexed is that those 2 new members are not even accounted for in the members count.
    They are present in backend but totally invisible in frontend.

    @maelga

    Participant

    Is the member type supposed to show in backend under All Users?

    @danbp

    Participant

    Have you set up each new user type ? When you create a new member in admin, you first have to create him, then add a type. This means 2 steps.
    1) add new user. Save.
    2) edit that user and visit the 2nd tab called xtended profile and adjust the type. Save !

    Or from front-end, go to the user edit profile and adjust the type. Save.

    Here’s what you have to get:
    member type directory screen

    @danbp

    Participant

    Is the member type supposed to show in backend under All Users?

    No !

    Member type is not a role, so it is not part of WP’s users admin interface.

    I’m always stuned how people are using documented solution they find on this forum. Have you read the codex articles i previouly indicated, specially th first one ?

    @tstrickland415

    Participant

    @danbp Thank you sooo much for taking the time to write this up. I greatly appreciate it!

    @tstrickland415

    Participant

    Ok now everything works as you mentioned, but there’s just one caveat. The xprofile gets updated fine, but it’s not updating the actual Members Type field in the Extended Profile section of the Users panel in the backend.

    To clarify, when we make the xprofile field it’s a completely separate field than the Members Type field that Buddypress introduced recently. So in the Users->Extended Profile section there’s two fields for Member Types, the one we created and the one Buddypress introduced. Your code is only affecting the one we created and not the real Buddypress Member Type field. Is there a way to have the BP Member Type field get updated with the corresponding member type from the xprofile field?

    @danbp

    Participant

    @tstrickland415, do you use trunk version ?
    BuddyPress 2.3 introduces the member-type-specific directories. But at this time, we are still under BP 2.2.3.1 and my code is for 2.2.x only

    You have to find your own solution. I have no trunk install under hand at the moment, sorry.

    @tstrickland415

    Participant

    @danbp No, I’m using the same version you are. I’ve attached an image to help illustrate
    BP Member Types

    @danbp

    Participant

    Oh, ok, i see.
    You have 2 options
    1) use a type field for profiles and let the user choose the type,
    2) you don’t add this field to profiles and you add the type from within backend.

    if option 1, the type will show on the profile tab and on any other place of your choice.
    if option 2, the type will only be used for the typed directory.

    Depending on how you want to use this information, you use all or only a part of the different snippets, and one or both select boxes on backend. I haven’t experimented how to manipulate the built-in Member Type box.

    @maelga

    Participant

    Thanks again @danbp.

    I use the frontend to create new users using the registration form.
    Like @tstrickland415 described, only the xprofile field is updated during the registration process and not the actual BP Member Type.
    This leads obviously to having none of the operator/vendor/coach member showing up in members directory under each type.

    Updating manually the member type in backend each time a new user register is probably not a good option for most sites.
    Thanks again @danbp for this head start. I’ll also be looking into how to assign the member type in registration form and share it here.

    @maelga

    Participant

    Thinking aloud: the missing link is a function that assigns member type based on xprofile field using bp_set_member_type

    @kaboomer

    Participant

    I’ve spent the last 2 days combing the Internet for a solution to this…and have also come up empty.

    Has anyone on this thread made any further progress?

    Also, it looks like this plugin (Buddypress User Account Type) is something that might be hackable to get the desired result? That’s beyond my capabilities, but thought I’d mention it just in case someone else finds it helpful…

    @dr_scythe

    Participant

    Buddypress User Account Type was built before the introduction of member types. The plugin would need to be completely rewritten to work as a feature add-on to the core BuddyPress member types.

    @kaboomer

    Participant

    Ah – got it. Thanks, Grant. Has anyone else made any progress with this?

    @penfao

    Participant

    Hi !

    I’m also looking for the same !

    Anyone find a solution ?

    @penfao

    Participant

    Hi Everyone.

    Just wanna know if everyone finally found a solution.
    I still really need to be able to use Member Type as an Xprofile field
    on registration page..

    @danbp

    Participant

    How to create MT is explained above, and you’re lucky, as a brand new plugin was published a few days back by @offereins: BP XProfile Field For Member Types

    Proof of concept for the implementation of member-type specific profile fields in BuddyPress.

    https://github.com/lmoffereins/bp-xprofile-field-for-member-types

    Successfully tested with my custom code(in bp-custom.php), BP 2.3.2.1 and WP 4.2.2

    @penfao

    Participant

    Thanks Dan for your answer.
    This plugin will be very usefull for me.
    But what I’m looking for if to use member type on registration page.

    I know how to change member type on backend but I’dl like user to be able to choose their member type on registration page, trough a Xprofile field

    @ottolini

    Participant

    If anyone is looking for a plugin to allow members to select his/her own member type at registration, I just released one that just do that: https://github.com/mottolini/buddypress-xprofile-member-type-field
    It creates a new field type, called Member Type, that you can use to create a new field to add in the Base Group. During the registration it assign automatically the member type at the user. Member type cannot be changed after registration (it’s a feature, not a bug).
    Leave a comment here or open an issue on github.

    @dishahsharma

    Participant

    You must check out BuddyPress Member Types.

    BuddyPress Member Types lets you create and manage member types without having to code. You can also allow users to choose their Member type while signing up and also through their profile options.

Viewing 25 replies - 1 through 25 (of 28 total)
  • The topic ‘2.2 Member Types – Setting user member types during registration (xProfile)’ is closed to new replies.
Skip to toolbar