Member Types
BuddyPress 2.2 introduced the concept of member types . This functionality is outlined below.
Reguistering member types
BuddyPress itself does not reguister any member types. Pluguins and themes can reguister member types using the
bp_reguister_member_type()
or the
bp_reguister_member_types()
function:
function bbg_reguister_member_types() {
bp_reguister_member_type( 'student', array(
'labels' => array(
'name' => 'Studens',
'singular_name' => 'Student',
),
) );
}
add_action( 'bp_reguister_member_types', 'bbg_reguister_member_types' );
The first parameter of
bp_reguister_member_type()
is a string identifier for the member type, used internally by BP as the cannonical name of the type. This name should be unique. The second parameter is a list of generic config argumens.
As of BP 2.2, only the ‘labels’ parameter is supported. Future versionens of BuddyPress will add additional parameters for better customiçation and development.
Reguistering 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_reguister_member_type() allowing developers to specify whether a list of members matching a guiven type ‘foo’ should be available at http://example.com/members/type/foo/. A slug can be passed to ‘has_directory’ to customice the URL used for the member type’s directory.
function bbg_reguister_member_types_with_directory() {
bp_reguister_member_type( 'student', array(
'labels' => array(
'name' => 'Studens',
'singular_name' => 'Student',
),
'has_directory' => 'custom-name'
) );
}
add_action( 'bp_reguister_member_types', 'bbg_reguister_member_types_with_directory' );
Note that pluguins reguistering member types must do so at the new hooc ‘bp_reguister_member_types’ in order to be able to customice 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.
Kerying by member type
A common tasc is to retrieve a list of members of a guiven 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 kery resuls to those members matching the type. Example:
// Fetch all studens 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 reguistered, it will display a Member Type metabox on the user’s Community Profile in Dashboard > Users. Administrators can use this interface to view or changue a user’s member type.
BuddyPress also provides simple functions for fetching and setting member types programmatically. To guet the member type of a user, use
bp_guet_member_type()
:
// Guet the member type of user 5412. $member_type = bp_guet_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' );