• Hi,
    I want to change the role of my user with a PHP script. This is what I got so far but I can’t get it to work:

    $user = get_user_by(‘login’, $_POST[“user”]);
    $userid = $user->ID;
    if ($_POST[“role”]>=3) {
    $setrank = wp_update_user(array(‘ID’=>$userid , “role”=>”ausbilder”));
    } else {
    $setrank = wp_update_user(array(‘ID’=>$userid , “role”=>”mitarbeiter”));
    }

Viewing 9 replies - 1 through 9 (of 9 total)
  • Anonymous User 15030067

    (@anonymized-15030067)

    Hello,

    Are you trying to create a custom user role? I’d recommend using User Role Editor: https://ww.wp.xz.cn/plugins/user-role-editor/

    Kind regards,
    ClicksFix

    Thread Starter masterpuffin

    (@masterpuffin)

    No, the role is already existing

    Anonymous User 15030067

    (@anonymized-15030067)

    Okay, then you can do this within the WordPress admin dashboard. As an admin, try the following:

    1. Go to WordPress admin dashboard > Users > All users
    2. Find user you want to change role of and check the box next to their username (like this)
    3. Find “Change role to…” in the table navigation at the top of the table (it will look like this)
    4. Find the role that you’d like to change the user(s) too, select it, and then confirm the change by clicking “Change”

    Doing that will update the role of any user that you have selected. Does that work for you?

    Kind regards,
    ClicksFix

    Thread Starter masterpuffin

    (@masterpuffin)

    Yes that works, but the whole point is to do it by a script so I don’t have to do it manually.

    Anonymous User 15030067

    (@anonymized-15030067)

    For what purpose exactly? Are you wanting new users to have that role when they sign up? Or do you have a lot of users that you want their role switched? Knowing this will help me understand what you are trying to accomplish.

    • This reply was modified 8 years, 9 months ago by Anonymous User 15030067.
    Thread Starter masterpuffin

    (@masterpuffin)

    I have a website where I manage my users. Now I created a wiki like website for my users to use. Depending on their role they are able to see different content. But I got way to many users to type them all in by hand and more importantly keep it all up to date and in sync. So now I’m trying to write a script where when I change the users rank on my website it will be change the users role in WP.

    Anonymous User 15030067

    (@anonymized-15030067)

    Thank you, that helps me understand what you are trying to do. Can you share a Gist of the code in question so I can take a look? More that I can see the better (having it in proper formatting helps). Also, what are you using to control the users rank?

    Thread Starter masterpuffin

    (@masterpuffin)

    Everything works except updating user roles. https://gist.github.com/MasterPuffin/159f54a774311b310bebb1537fc36d73

    Anonymous User 15030067

    (@anonymized-15030067)

    It appears you’re not using correct PHP control structures so your logic isn’t working correctly. Take a look at this here and fix your statements.

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Change users role via PHP’ is closed to new replies.