BuddyDev

Using BuddyPress Member Types API like a Pro: Know Thy API

This is my third post in our series Using BuddyPress Member Types API like a Pro.  We will cover various available API functions for BuddyPress Member type in this post. This post is sort of documentation for the member type API.

Registering A Member Type:-

We covered this function in great details in our last post and I recommend reading the post.

Getting All registered member types:-

Parameters:-

  • $args :
    • Optional
    • Array
    • An array of key => value arguments to filter the member types
  • $output:
    • Optional
    • String  'names' or 'objects'
    • default is 'names'
    • Whether the function should return an array of member type names or member type objects
  • $operator:
    • Optional
    • String 'and' or 'or'
    • Used to match and filter the member type list based on first parameter

Example:-

 Getting a member type object:-

Parameters:-

  • $member_type: A valid member type name. Must be registered before using here.

Returns:-

Returns a registered member type object or null if not registered.

Example:-

 

Set A user's member type:-

Parameters:-

  • $user_id : Required. The ID of the user whose member type we are trying to set( e.g 1, 2 etc ).
  • $member_type:- Required. The member type that we are trying to set(e.g student)
  • $append:- Optional.  default is false. Use true if you do not want to reset the existing member type(s) of the user. False if you want the member type to be set to the given type only.

Example:-

 Get a user's member types:-

Get the associated member type(s) of the given user.

Parameters:-

  • $user_id: numeric ID of the user for which we are trying to fetch the member type
  • $single: Optional. default is true.  If true, will return an string with the first associated member type, else will return an arry of the member type names associated with the user

Returns:-

  • String: name of the member type if a member type is associated with the user and the parameter $single is true(which is default)
  • Array: array of string member type names if one or more member types are associated with the user.
  • false: if no member type is associated with the user.

Removing a User's Member Types:-

It is available since BuddyPress 2.3. Removes the associated member type for the given user.

Parameters:-

  • $user_id: Numeric ID of the user
  • $member_type: name of the member type.

Returns:-

boolean or WP_Error. True on successful removal. False or WP_Error on failure.

 

Checking if a user has a specific member type:-

Check if a user has a given member type. It is one of the most useful function introduced in BuddyPress 2.3

Parameters:-

  • $user_id : numeric ID of the given user
  • $member_type: The member type nae wer are checking for.

Returns:-

true if the member type is associated, false if not.

Example:-

In the above example, you may write a function(I prefer as that makes more sense) to check if the user is student and use it as shown. you can do it for other types as well.

Getting the current Member type on directory:-

This function should only be used on members directory. It will return the current member type being viewed.  For example, if you are viewing http://example.com/members/type/student it will give 'student'.

Bonus:- Change member type base url.

In case you want to change the directory base prefix for member type listing page from 'type' to something else, you can filter on 'bp_members_member_type_base' and return the replacement name.

Example:-

In the above snippet, I have changed the prefix from 'type' to 'user-type'. You can change it to anything you want.

The Last Thing: How does the member type object looks like?

If you are confused about how does a member type object look like, Let us take a look at this student example. Doing a print_r on the object gives the following result.

As you see, It is an object of the generic class stdClass with four properties.

  • labels: An associative array of plural name(specified by key name) and singular name.
  • has_directory: boolean. Whether directory page is enaled for this type or not.
  • name: the key you used as the identifier for this member type
  • directory_slug: The string that comes after the member type prefix in the directory url.

Have fun and let me know how do you plan to use BuddyPress Member type API. In our next and final tutorial in this series, we will cover using member type for building gender specific directories for dating type website.

2 Responses to Using BuddyPress Member Types API like a Pro: Know Thy API

  • Thanks for excellent pluging…

    But, How I can do to add several members types to several user in Dashboard at same time?

    This because I have 120 users and I have 10 member types.

    Thanks you

    • Hi Luis,
      Thank you.
      Current BuddyPress allows you bulk updating member types from the Dashboard->Users screen. You will see the member type dropdown there and you can do it for 20 at one time.

      Hope that helps.