BuddyDev

Using BuddyPress Member Types API like a Pro: Registering new Member Types

This is part two of our series Using BuddyPress Member Types API like a Pro. If you haven't read the introduction, Please do read it here.

Assumption:- I am assuming that you are familiar with either developing theme or plugin. The code in this tutorial and following tutorials can be put in your custom plugin, or bp-custom.php.

To register a new member type, we will use the function bp_register_member_type. Please note that you should register a member type  on 'bp_register_member_types' action.


For BuddyPress 2.2 you may want to use bp_init hook instead( 'bp_register_member_types' action does not exist in 2.2. In BuddyPress 2.3 if you register at bp_init hook, the member type directory will not work, so stick with 'bp_register_member_types' ).


Before discussing more about this function, Let us see an example. In the code below, I am registering two member types 'student' and 'faculty'.

As soon as you put this code in your bp-custom.php or the plugin file as discussed above, the two member types will be available. You can easily verify that the member types are registered,  by visiting Dashboard->Users and clicking on the Extended profile of a user. You will see a drop down list in the right side like this:-

member-types-list

Congratulations, you just registered two new member types. You can also assign a member type to the user by selecting it and clicking Update Profile button. I just updated the member type to student. We will learn how to assign member types using code in part-3 and 4 of this tutorial.

You can access the list of all student by appending 'type/student/' to your member directory url( Only in BuddyPress 2.3+ ). For example, if your member directory url is http://example.com/members/ you can see the list of students by visiting http://example.com/members/type/student/

Here is a screenshot:-

bp-member-type-directory

 

As you see, we have appended 'type' and then the actual member type. So the format is like {directory_url}/type/{$member_type}/

Now, we have already done enough work, so let us decode the function.

Usage:-

 Parameters:-

$member_type

  • String( Required ) :- A unique key to identify the member type. It must not have spaces.  Best practice is to use alphabet and lowercase, singular noun like 'student', 'staff', 'faculty', 'male', 'female', 'actor', 'director' etc

$args

  • array( optional ):- An associative array of arguments. It has  two allowed keys at the moment 'labels' and 'has_directory' ( has_directory was added in BuddyPress 2.3). These are explained below
  • labels(optional):- An array of singular and plural string which is used at various places to provide descriptive names. Example:-

     
  • has_directory( optional ) : (Added in 2.3) Boolean or String. Default is true. You can use false to disable the member type directory.  You can specify a string that you can append to the directory_url/type/{$has_directory_string_name} to see the member type directory. If you don't specify it or set it to true, you can access the member type directory by visiting your member_directory_url/type/$member_type_name.

Tip:- You can always see all members of a type by appending a query string ?member_type={$member_type} in your member directory url. Example:- http://example.com/members/?member_type=student will list all students.

I hope this tutorial helps you to understand member type registration properly.

We will explore various available member type API functions and their use in our next tutorial( coming Monday ) .

7 Responses to Using BuddyPress Member Types API like a Pro: Registering new Member Types

  • This is great! Thanks a lot Brajesh!

  • Hi. thanks for this article. I tried to putt this on my bp custom but my site is a blank page when I do that.

    I also tried

    'Accountants',
    'singular_name' => 'Accountant'
    );
    $types['designers'] = array(
    'name' => 'Designers',
    'singular_name' => 'Designer'
    );
    $types['sales_agents'] = array(
    'name' => 'Sales Agents',
    'singular_name' => 'Sales Agent'
    );
    if(!empty($types)){
    foreach($types as $key=>$type){
    bp_register_member_type( $key, array(
    'labels' => array(
    'name' => $type['name'],
    'singular_name' => $type['singular_name'],
    ),
    ) );
    }
    }
    }

    add_action( 'bp_init', 'mpl_register_member_types');

    ?>

    on my bp custom, and I see the Member Type on Extended Profile, but not on my register page, and directories don't work 🙁

    Any Help ?

  • complete code :

    'Accountants',
    'singular_name' => 'Accountant'
    );
    $types['designers'] = array(
    'name' => 'Designers',
    'singular_name' => 'Designer'
    );
    $types['sales_agents'] = array(
    'name' => 'Sales Agents',
    'singular_name' => 'Sales Agent'
    );
    if(!empty($types)){
    foreach($types as $key=>$type){
    bp_register_member_type( $key, array(
    'labels' => array(
    'name' => $type['name'],
    'singular_name' => $type['singular_name'],
    ),
    ) );
    }
    }
    }

    add_action( 'bp_init', 'mpl_register_member_types');

    ?>

    • Hi Vincent,
      Please make sure you pass the parameter 'has_directory' => true to enable the directory.
      Also, you will note in my post above that the directories won't work if you register on bp_init action. You should attach to bp_register_member_types action. Also, directory will only work on BuddyPress 2.3 or above.

      The member type won't appear on registration page automatically, We will cover that in our 4th part(By creating a custom xprofile field ).

  • hi i am new to buddypress how to make this membership i dint understand where to put the code