Codex Home → Developer Resources → Member Types
Member Types
BuddyPress 2.2 introduced the concept of member types. This functionality is outlined below.
Registering member types
BuddyPress itself does not register any member types. Plugins and themes can register member types using the bp_register_member_type() or the bp_register_member_types() function:
function bbg_register_member_types() {
bp_register_member_type( 'student', array(
'labels' => array(
'name' => 'Students',
'singular_name' => 'Student',
),
) );
}
add_action( 'bp_register_member_types', 'bbg_register_member_types' );
The first parameter of bp_register_member_type() is a string identifier for the member type, used internally by BP as the canonical name of the type. This name should be unique. The second parameter is a list of generic config arguments.
As of BP 2.2, only the 『labels』 parameter is supported. Future versions of BuddyPress will add additional parameters for better customization and development.
Registering a member type will also enable a meta box so administrators can set a member』s type when editing a user』s 「Extended Profile」 in the WP admin dashboard.
Member Specific Directory
BuddyPress 2.3 introduces the member-type-specific directories. It adds the new 『has_directory』 argument for bp_register_member_type() allowing developers to specify whether a list of members matching a given type 『foo』 should be available at http://example.com/members/type/foo/. A slug can be passed to 『has_directory』 to customize the URL used for the member type』s directory.
function bbg_register_member_types_with_directory() {
bp_register_member_type( 'student', array(
'labels' => array(
'name' => 'Students',
'singular_name' => 'Student',
),
'has_directory' => 'custom-name'
) );
}
add_action( 'bp_register_member_types', 'bbg_register_member_types_with_directory' );
Note that plugins registering member types must do so at the new hook 『bp_register_member_types』 in order to be able to customize the 『has_directory』 value (from its default of true). bp_has_members() automatically detects the presence of a member type in a URL. When no member type of the form example.com/members/type/foo/ is found, URLs of the form example.com/members/?member_type=foo will be detected.
Querying by member type
A common task is to retrieve a list of members of a given type (or set of types). bp_has_members() and BP_User_Query accept a 'member_type' parameter, which can be a single member type or an array/comma-separated list of member types. This will filter query results to those members matching the type. Example:
// Fetch all students and teachers.
$member_args = array(
'member_type' => array( 'student', 'teacher' ),
);
if ( bp_has_members( $member_args ) ) { // ...
Fetching and setting individuals』 member types
When BuddyPress detects that member types have been registered, it will display a Member Type metabox on the user』s Community Profile in Dashboard > Users. Administrators can use this interface to view or change a user』s member type.
BuddyPress also provides simple functions for fetching and setting member types programatically. To get the member type of a user, use bp_get_member_type():
// Get the member type of user 5412.
$member_type = bp_get_member_type( 5412 );
Set a user』s member type using bp_set_member_type():
// Set the member type of user 5412 to 'student'.
$member_type = bp_set_member_type( 5412, 'student' );