1

I'm trying to promote my Wordpress users when they visit certain link but I'm not sure how to do that.

I found this function and placed it on the page where I want that change to be made. I though it might do the trick, but it won't:

function update_roles() {
   global $wpdb;
   $author = wp_get_current_user();

  $author->remove_role( 'subscriber' );
  $author->add_role( 'contributor' );
}

Any clues on how to do this?

icedwater
  • 4,701
  • 3
  • 35
  • 50
DearBee
  • 416
  • 3
  • 20
  • How do you call the function? – Phill Healey Feb 15 '18 at 21:26
  • @PhillHealey I've just placed it into a shortcode and onto my page. Do I need some onload parameter in order for it to work? I don't know much about PHP – DearBee Feb 15 '18 at 22:05
  • In that case I'd need to see all the relevant code. Can you setup a gist or codepen and put it all in there, then I and others can see exactly what you are doing. – Phill Healey Feb 15 '18 at 22:24
  • @PhillHealey I would but this is basically it :) I took it from here and stripped the posts condition, because I don't really need condition, since my only condition is that they are on the right place: https://stackoverflow.com/questions/9181440/wordpress-change-user-role-conditionally – DearBee Feb 15 '18 at 22:40
  • So how do you call the function? Whats the code for your shortcode? How & where are you using the shortcode? We need to see the code. – Phill Healey Feb 19 '18 at 09:45

1 Answers1

2
wp_update_user( [ 'ID' =>get_current_user_id(), 'role' => 'contributor' ] ); 

You can use this code in your page template to change your user role.

Beso Kakulia
  • 556
  • 4
  • 13