Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] Add role to user depending on value in profile field on registration


  • DanielEngelhardt
    Participant

    @danielengelhardt

    Howdy,

    i would like to add a user role to a new user during registration, depending on a fields value in the registration form.
    example: on my registration form a user can choose between two values via radio buttons. If he choses value A, role A should be added to this user. If he choses B, role B should be added to the user.

    How could i do that? I read this: http://octalforty.com/articles/assigning-role-on-wordpress-registration-profile-page/ but it aims at the wordpress registration form and does not use buddypress extended profile fields.

    Thanks for any help
    Daniel

Viewing 5 replies - 1 through 5 (of 5 total)

  • danbp
    Moderator

    @danbp

    WordPress is the software behind buddyPress.
    If you need custom roles&capabilities on your site, you can add roles programatically as explained on your mentionned tutorial. If it works, site admin can attribute one of the additionnal role to members, within admin.

    BuddyPress, at this time, handles only member types. A member type is not a role or a capability à la WordPress, but only a way to sort better your members. In other words, a kind of a member category.

    To handle such behave from front-end while registering, you can associate a xprofile field value with an existing WP role. See here for a solution (untested) provided a few month ago.
    https://buddypress.org/support/topic/how-to-assign-a-wp-user-role-based-on-a-registration/#post-232887

    Add snippet to your child theme functions.php or into bp-custom.php and give it a try.


    DanielEngelhardt
    Participant

    @danielengelhardt

    yay, that was what i was looking for. Dont know why i didnt find it myself – sorry


    danbp
    Moderator

    @danbp

    Give feedback here if it worked for you.


    DanielEngelhardt
    Participant

    @danielengelhardt

    Yes it worked for me, thanks again. Here is the snippet which i am using in my functions.php:

    add_action('bp_core_activated_user', 'bp_custom_registration_role',10 , 3);
    function bp_custom_registration_role($user_id, $key, $user) {
       $userdata = array();
       $userdata['ID'] = $user_id;
       $userdata['role'] = xprofile_get_field_data('Ich bin', $user_id);
       
       if ($userdata['role'] == 'Dienstleister') {
            $userdata['role'] = 'dienstleister';
       }
       elseif ($userdata['role'] == 'Kunde') {
            $userdata['role'] = 'kunde';
       }
       //only allow if user role is my_role
       if (($userdata['role'] == "dienstleister") or ($userdata['role'] == "kunde")) {
            wp_update_user($userdata);
       }
      }

    danbp
    Moderator

    @danbp

    Yep ! Glad yo got it. Thank you for feedback. 😉

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] Add role to user depending on value in profile field on registration’ is closed to new replies.
Skip to toolbar