This Stack response explains how to change the role of a given user. Is there a way to execute that when the page loads?

I run a language school and there are unique login destinations for Prospective, Active and Inactive students depending on the desired action that I want them to take. Once they make a purchase, I would like them to become Active so that they're not, for instance, looking at taking a trial lesson.

3 Answers 3



I updated the function to check for role for which we want to make changes for, if the current user role is not within our roles_to_change array then the function exits.

Note that I haven't tested for bugs, so if you find any, just report them here.

@Malisa's answer is a good approach. However, if you still want to hook on page load, you could try the template_redirect hook.

You would need some kind logic to only run the code on specific use case. So assuming you already have that logic AND that the user is already logged in when you try to change his role.

something like this should work

add_action( 'template_redirect', 'my_change_user_role' );
function my_change_user_role(){

  $current_user = wp_get_current_user();

  // Bail if no one is logged in
  if( 0 == $current_user->ID )

  // Fetch the WP_User object of our user.
  $cur_user = new WP_User( $current_user->ID );

  // Only Apply to users within these roles
  $roles_to_change = array(
  $make_change = false;  // Flag telling to make the changes or not

  // Check if we can make the change to the current user
  foreach( $roles_to_change as $change ){
    foreach ( $cur_user->roles as $user_role ){
      if( $change == $user_role ){
        $make_change = true;

  // Bail if role is not within those we want to make a change for
  if( ! $make_change )

  // Replace the current role with our 'new_role' role
  $cur_user->set_role( 'new_role' );      


If you use this in a form you could use the $_GET or $_POST superglobal to pass on some information about the user (like is ID for instance).

  • Thank you bynicolas, this is great! And is there any way to insert a line that says essentially "only do this change if they are currently in the X, Y or Z role"? These pages will be protected however I don't want to have future web designers or site testers wondering why they've been demoted. Grateful for any suggestions. I agree that @Malisa is onto something in the sense that it answers the specific example that I gave, but there are several good reasons for doing it this way. Certainly this code could be adapted. Commented Sep 13, 2016 at 16:00
  • 1
    Yes you could only apply this to users within a specific role. Just add an additional check within the function. Check my answer I will update it with a way of doing this.
    – bynicolas
    Commented Sep 13, 2016 at 16:03
  • Edited the answer @endofthecode
    – bynicolas
    Commented Sep 13, 2016 at 16:22

I would suggest, rather than trying to do this on a page load, adding an extra function to the purchase code you have. Here you will already have the user_ID and data available. You then just use wp_update_user on purchase completion.

  • This is to avoid issues with unconfirmed transfers and to avoid using an upgraded plan with our payment provider. Additionally, there are non-payment use cases for this and the underlying technical feature is transferable to those situations. Commented Sep 13, 2016 at 13:12
  • By transfers, I'm assuming you mean "payments made", do you not get a callback from your payment processor saying whether confirmed or not? If it s none payment cases, you obviously bypass the checkout and straight to confirm? Difficult without indepth explanation
    – Malisa
    Commented Sep 13, 2016 at 13:24
  • Respectfully, I appreciate your help but there are multiple reasons that led me to asking about doing this when the page loads (several of which are mentioned above) after several months of trying to figure this out by myself. I hope that the conversation can continue along the stated purpose because it is very difficult to ask a question twice on Stack. Commented Sep 13, 2016 at 15:55
  • 1
    @endofthecode No problem at all, sorry if I have got your questions somewhat side tracked.. I'll leave it with you.
    – Malisa
    Commented Sep 13, 2016 at 16:01
  • Very dearly appreciated. Commented Sep 13, 2016 at 16:02

Visit User Role Editor. It's a plugin that are provided to change user role.

For the custom code you can find out in wp_update_user.

  • 1
    Thank you @vivek, I appreciate it. I would give you an upvote but I don't yet have the reputation points on here to do so. Commented Sep 13, 2016 at 16:01
  • @endofthecode it's ok mate Commented Sep 14, 2016 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.