Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to assign a WP user role based on a registration

  • @spiritix

    Participant

    Hello everybody

    I’d like to ask you for a help with assigning a WP user role based on a BuddyPress registration.
    REASON: I do this because I want to have an automatic way of allowing users after their registration to see and contribude only to specific forums.

    For subscribers I have Forum1 ready,
    for contributors Forum1 AND Forum2.
    Subscribers cannot see the content of Forum2!

    Everything should be automatically done. User comes to the website, registers (chooses type of account), gets access to specific forum.

    If you could suggest a better solution, OK, if not, plese, try to fix this:

    I have BuddyPress installed. I used extended profile fields to create a list of different users, let’s say User1, User2, User3. After hitting “finish registration” button, based on a selection of User1 or User2 or User3, specific role is assigned. So for example:

    If User1 is chosen, user gets WP role subscriber,
    if User2 is chosen, user gets WP role subscriber,
    if User3 is chosen, user gets WP role contributor.

    Something similar is mentioned below with Cimy User Extra Fields, but it does NOT work with BuddyPress registration. If you add anything with Cimy User Extra Fields, you cannot see it in BuddyPress registration.

    What function/code, to what file I should add so I could have my solution?
    Thank you.

    ———————————————————————-

    
    //
    // Modify registration form to include roles
    //
    add_action('user_register', 'register_role');
    
    function register_role($user_id, $password="", $meta=array()) {
       $userdata = array();
       $userdata['ID'] = $user_id;
       $userdata['role'] = $_POST['cimy_uef_USERTYPE'];
       if ($userdata['role'] == 'A Contributor') {
          $userdata['role'] = 'contributor';
       }
       if ($userdata['role'] == 'An Editor') {
          $userdata['role'] = 'editor';
       }
    
       //only allow if user role is my_role
       if (($userdata['role'] == "contributor") or ($userdata['role'] == "editor")){
          wp_update_user($userdata);
       }
    }
    
Viewing 10 replies - 1 through 10 (of 10 total)
  • @spiritix

    Participant

    btw – how can I have so nice BuddyPress members section?

    Members

    It’s awesome!

    @spiritix

    Participant

    Never mind, I think I have it 🙂
    [edited – please use the ‘code’ button to share snippets]

    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('Typ konta', $user_id);
       
       if ($userdata['role'] == 'User1') 
          $userdata['role'] = 'subscriber';
       
       if ($userdata['role'] == 'User2') 
          $userdata['role'] = 'subscriber';
       
       if ($userdata['role'] == 'User3') 
          $userdata['role'] = 'contributor';
    
       //only allow if user role is my_role
       if (($userdata['role'] == "subscriber") or ($userdata['role'] == "contributor"))
          wp_update_user($userdata);
       
      }

    @youmin

    Participant

    See this ticket.

    https://buddypress.trac.wordpress.org/ticket/6154#ticket

    Since buddypress 2.2 released we can tightly integrate user roles based on their profile selected. As well their is a hook . if you know php you can create a snippet so that others can get benefits along with you.
    I think same above codes with a little modifications we can hook it to bp_set_members_type at line 2576.

    @mcpeanut

    Participant

    Hey @youmin and @spiritix i hear you both on this matter, after much testing myself on how to do this i have realized the best way to do this is via a few other plugins.

    firstly i would recommend using a membership plugin and a plugin to create your own registration forms as well as a roles capability management plugin, you can then overide the buddypress registration process (but you will lose the ability of integrating the custom fields you create for the buddypress registration unless you tie them into your own registration forms)
    but the way i got around this myself without the problem of having to tie them with my registration forms was to add a buddypress profile progression plugin so once they have registered they will see a notice in the header showing the percentage of the profile they have completed encouraging them to fill the fields in “you could even integrate it with an awards system the more they fill in” its up to you. (to me this works perfectly fine as i can still add as many buddypress fields as an when i want and have a simple registration process to boot).

    if you mess around with this idea you will see that assigning a user to a specific role does infact work with the right combo of plugins and effort, i know this seems a long haul work around but the way i have set it up works fine for me. i may go into more detail for you guys when i have more time and explain exactly how mine works. plus this way if you do decide to implement some kind of pay per view membership for higher levels in the future your ready to go!

    @youmin

    Participant

    Thank you mcpeanut for you research and work, I am glad to see details of your work since I am struck at this point I am willing to impliment it soon. Plz make a time to explain in details.

    BTW have you seen the ticket I had raised ? I am curious about that ticket just because of the new introduced api member_type and the person who reviewed ticket is respectable Chief Dev of buddypress, as per his comment its take 6 lines of codes, to tie WP roles with member_type code. So it means Two solutions at one shot.

    Thank you again.

    @youmin

    Participant

    Hey guys dev Henry Wright had replied to this topic but its not showing here strange!!
    https://buddypress.org/members/henrywright

    But you can see his solution in his profile link provided above, he rectified error

    @henrywright

    Moderator

    Hi @youmin

    My post was this:

    Just to point out, you can’t use or in PHP. Instead you should use a double pipe like this ||.

    This is wrong! You can in fact use the or operator in PHP, it just takes a lower precedence than ||.

    Hence why I deleted my post.

    Hope that clears things up.

    @mcpeanut

    Participant

    @youmin to explain it in detail all the steps i have taken to do it the way i have will take me a while to type out because it means using and altering code for 4 seperate plugins, tbh even if there is now an easier way to do it i dont think i would use it because after all the time ive taken setting it up this way i am really happy with how it all works on my website.

    The plus side of doing it this way for me is having the ability to also have extra membership levels in the future that i could in-fact charge a fee for access to certain parts of my site or give them extra privileges or could even assign donators with extra privileges etc etc, the possibilities are endless for me the way its setup now, but it just takes time to set it all up.

    like i said im really to busy to go into full detail at the minute due to finishing off all the bits and bobs before launching my website.

    If you decide to take this route instead of the route your going all the clues are there for you in my earlier response, it depends on what you actually want to use the extra levels for i suppose and if you will make use of them. there are a number of free membership plugins about if you search but that will be down to your own personal preference on which to use, same goes with the other plugins i mentioned as there are numerous plugins for these tasks if you take this route.

    @spiritix

    Participant

    Hello guys

    What I used is:
    – BuddyPress with combination of either bbP private groups plugin or Members plugin or Restrict Content Pro – bbPress plugin
    – I created extended BuddyPress profile fields in registration
    – I restricted access to the forum with one of above mentioned plugins
    – I used my code mentioned in the beginning

    How it looks like:
    – my code works like a charm! once you choose User1 type in registration, it applies subscriber role, User2 subscriber as well, contributor for User3 – THIS WORKS.
    – what does NOT work is restriction via plugins. Have no clue why. With members plugin od pivate groups, I set Forum1 to be accessable ONLY to User3 type of user, but User1 and User2 can access too! Why?! It has nothing to do with my code, after registration you can clearly check it in WP Users that the role has been assigned.

    If I knew hot to restrict access to the forum based on the WP Role, my code would be sufficient to solve this. Can you help with that part?


    @youmin
    – are upi saying in BB 2.2 access to forum based on the WP role is fixed?

    @mcpeanut

    Participant

    @spiritix usually with any kind of members plugin you can restrict access per user level on a page or post basis and should have the option within that post or page to select what user level has access to it with a custom message and even a link pointing them to upgrading their account.

    what membership plugin are you using? and im sorry im not familiar with restrict content pro or bbpress though as ive not used them, if your users get given the correct roles within wordpress after registration then the problem lies with one of these plugins or some kind of conflict is my guess, maybe try to track it down by deactivating all plugins and reactivating them separately.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘How to assign a WP user role based on a registration’ is closed to new replies.
Skip to toolbar