BuddyDev

Assign user roles based on BuddyPress profile field data on user registration

Are you looking for how to assign role to user based on profile field data for BuddyPress registration? It’s simple.

We can use the “bp_core_activated_user” action which is called when user account gets activated. Since we plan to assign role based on profile field data, we will need to find the data.

To fetch BuddyPress profile field data, we will use xprofile_get_field_data() function.

Now, let us see an example of assigning role based on BuddyPress profile field data. For this example, I am creating a field named “Membership Type” as shown below

You can use any single valued field(Radio, select, text etc.).

Once I created it, the field became available on my registration page as shown below

Based on the above setup, here are the assumptions I am using to write the code

  • We have a profile field with certain values
  • We know what role we want to assign for these values

In my case, I wanted to assign “Contributor” for the Student and “Autohor” for Teacher.

Here is the actual code. Please put it in your bp-custom.php

I have commented the code and explained as much as I can there. You will need to change the Filed Name, roles and the values in condition based on your use case.

Please feel free to use it, extend it as you please. If you need further help with the code, Please do post in our support forums.

Also, if you are looking for more control, You may try our BuddyPress Member Types Pro which allows you to assign member type, role, groups etc on BuddyPress user registration.

One Response to Assign user roles based on BuddyPress profile field data on user registration

Leave a Reply

Are you looking for support?

Please ask support questions in BuddyDev Forum and help us serve you better. Please use comment for general discussion/feature suggestions.

Your email address will not be published. All the fields marked as * are required.

If you are posting code, please use <pre> { Your Code Block } </pre>. Example:- <pre> echo $name; </pre>

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">


Subscribe