2

I have two user role

1)student

2)paid student

I have situation after the payment i am changing role student to paid student it work good but here i have also change the forum role programmatically to participant for paid student here . How do i change forum role programmatically using query, function anything.

Below code i have used to change user roles programmatically it works good but i am stuck d in changing forum role. i am looking something like this.

$user_id = get_current_user_id();
                        $oldrole=implode(', ', $user_id->roles);
                        if($oldrole=="student")
                        {
                        $u = new WP_User($user_id);

                      // Remove role
                       $u->remove_role('student');
                             $newrole="paid_student";
                       // Add role
                        $u->add_role( $newrole );
                        } 
Cœur
  • 37,241
  • 25
  • 195
  • 267
Sanjay Nakate
  • 2,020
  • 6
  • 39
  • 74

1 Answers1

1

Finally i have got the answer. Bellow are the step.

1) Get user current user id.

<?php $user_id = get_current_user_id(); ?>

2)set new user forum role you wish to change

<?php $new_role_forum_role="bbp_participant"; ?>

3)fire function.

<?php bbp_set_user_role( $user_id, $new_role_forum_role );?>

now check your user in back end and see forum user role.

Full snippet code

<?php
$user_id = get_current_user_id();
                $new_role_forum_role="bbp_participant";
                  bbp_set_user_role( $user_id, $new_role_forum_role );
                 ?>
Sanjay Nakate
  • 2,020
  • 6
  • 39
  • 74
  • I am looking to do the same thing as you, but I am not getting this to work. Where would you put this to get it to work? – David Avellan Sep 15 '15 at 21:19
  • 1
    @DavidAvellan every one have different situation when to run this code i have putted this code in payment gateway response file eg. if student role want to be paid membership once he get the paid member ship i wanted to assign his/her participant role so that i have putted this code in response file when the payment status marked as successful. if this is not working for you may be you are not getting user id or you code is not running you can debug you code using echo "whatever"; and add exit(); – Sanjay Nakate Sep 16 '15 at 04:39
  • @SanjayNakaye - I am trying to hook into this function unsuccessfully: function my_i4w_authenticated_remote_login($wp_user, $contact ){ // $wp_user is the user object (same as the standard $current_user) // $contact is an array with all contact record fields. $your_code_goes_here = true; } add_action('i4w_authenticated_remote_login', 'my_i4w_authenticated_remote_login',10,2); But doesn't do anything I can see. – David Avellan Sep 16 '15 at 13:02
  • 1
    can you run this funtion http://pastebin.com/pep3hxZr and upload the screen shot of and send link i want see output. – Sanjay Nakate Sep 16 '15 at 14:35
  • Displays a blank page with the number 6, which corresponds to the user ID of the member logging in. When I look up the user on the dashboard, they have no role assigned. However, on the bottom it has "Additional Capablities" as bbp_spectator: http://imgur.com/awMcO90 – David Avellan Sep 16 '15 at 14:46
  • select default forum role spectator and then run code with my old code from stack page – Sanjay Nakate Sep 17 '15 at 09:26
  • It was already set to default forum role spectator. Isn't that running exactly the same situation that we just did? – David Avellan Sep 17 '15 at 13:31
  • Ok, so after some more testing, I see it is in fact working, but there is a small issue. So when a user first registers, he starts as a forum role "bbp_spectator", which corresponds to my database tag, "guest". When he becomes a paying member, his new tag will be "member" and the forum role will be "bbp_participant". When they make the transition from guest to member, everything functions and appears correctly in user profile. However, when they revert back to "guest" (after canceling membership), it assigns the role of spectator, but it also adds: additional capabilities "bbp_spectator". – David Avellan Sep 17 '15 at 15:36
  • This seems to then stick onto the user, and if you switch roles back and forth, the additional capabilities of "bbp_spectator" will remain. Picture of it below: http://imgur.com/TakjUiK – David Avellan Sep 17 '15 at 15:37
  • This doesn't seem to affect the functionality, as the users still work the way they are supposed to. I do realize that all of the forum roles (besides blocked) do have the capability of "bbp_spectate", so I believe it doesn' make a difference. But I do find it strange that it happens, and would ideally like to not allow that. – David Avellan Sep 17 '15 at 15:44
  • Ok, I have it working 100% with this code. http://pastebin.com/DVAFyqhH I borrowed a bit of code from this answer: http://stackoverflow.com/questions/21012652/wordpress-change-forum-role-from-outside-bbpress-by-checking-user-status Now there are no additional capabilities listed, and everything works as expected. I am not sure why it works, or why your code would have additional capabilities created to begin with called "bbp_spectator" (which isn't a capability to begin with - it is a role), but at least it works. – David Avellan Sep 17 '15 at 16:18
  • great may be you need to include of the file bb press one to make workable that code. – Sanjay Nakate Sep 18 '15 at 10:39