forum, Been searching for some answers about changing

Hello forum,

Been searching for some answers about changing a user's role by clicking on a button on an upgrade page. I need a free upgrade button that I will add style to, and I already have a paid upgrade button.

Does anyone know how to code a function to change a user's role with the click of a button? I know this may seem jovial, but I'm not the best at php.

Thank you for any support!

  • Tyler Postle

    hey Josh,

    Hope you're doing well today and thanks for your question!

    To be honest, I don't think that is too jovial of a request. In order for a user to change their user role they would need the capability to do so, which is usually reserved for Admins of the site - as you wouldn't exactly want the basic user to have that sort of power. Otherwise they could even change the admins role.

    May I ask why you're wanting to allow your users to do this? Perhaps there is another solution we could use :slight_smile:

    Look forward to your reply!


  • josh_redler

    Hi Tyler,

    Thanks for your reply, perhaps I didn't explain it correctly. I am building a multisite community using buddypress. Each site will have their own groups, events, activity feeds and other buddypress functions.

    What I am trying to do is make users sign up to the main site and then choose a site they want to join (can be several). I have tried "Join My Site" plugin, doesn't work with buddypress. I then tried "Buddypress Multisite Network" combined with "Join My Blog", but after many attempts of trying to get them to work I found that "Buddypress Multisite Network" messed with my install in some cases and other tries I found it did nothing. Perhaps I am doing something wrong there, but I did not find it worked for me.

    So my work around to this would be when a user signs up to the main site they would be given access level 0 and able to see only certain pages on all sites. Then when they go to the 'Join' page there would be a button that allows them to click and be upgraded to level 1 which has certain permissions. There will also be a paid upgrade on each site for extra functionality, but I want users to be able to join as many sites as they want for free.

    I do not want users to be able to choose what level they will become, I want all of that to work on the backend and they just get more permissions (by going to a certain member type) when clicking that button.

    What would you suggest?


  • Michelle Shull

    Hey there, Josh!

    I think you can probably do this with Membership, although not directly out of the box. I'd set up three Access Levels, one for each of your two free levels, then one for the paid level. Subscribing would put users in the first free level, then they could update to free level #2 from a subscriptions page, which would have no charges attached. Then you can add a third level, with all the content for that level protected, and allow members of the second free tier to sign up, pay, and subscribe.

    I am a little curious about why you would want an extra layer of friction between your two free tiers, is there a particular advantage to forcing them to complete another step before they can see other free content?

    Hope this helps!

  • josh_redler

    Hi Michelle,

    Thank you for your reply. I tried Membership plugin but didn't find it integrated well with what I was doing, and I found s2member framework to do what I needed so at the moment I'm gonna stick with it.

    The reason being, I am developing a community that is separated by neighbourhoods. I do not want users who register to have the same access across all sites. Users will only be able to see sub sites when logged in. To gain more access to a specific sub site they will need to 'Join' the site which will give them a second level of membership on that specific site, but not full access to certain features which are for paying members of that site.

    I know this may sound confusing, but I did figure it out in the end with some help from another developer friend :slight_smile:

    function your_function_name() {
    $user_id = get_current_user_id();
    $blog_id = get_current_blog_id();
    $role = 's2member_level1';
    add_user_to_blog( $blog_id, $user_id, $role );

    And then I called the function in a form...

    <form method="post" action="">
       <input type="submit" name="join_nh" value="Join" />
       <?php if (isset ($_POST['join_nh'])) {
    	   echo 'You have just joined this site! <a href="';
    	   global $current_user; echo home_url() . '/members/' . $current_user->user_login . '/profile/';
    	   echo '">See your profile</a>';
    	   } ?>

    That all seemed to work!

    Now I need to implement this into Blogs Directory for all users with member types X, Y, and Z. Will start another topic for this one.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.