3

I want to programmatically add the current user to a role and also remove the current user from a role in a php page i created . am really stuck. thanks in advance

2 Answers 2

5

You can do that like this :

$my_user = new WP_User( $user_id );
$my_user->set_role( "editor" );
0

Just fleshing out the previous answer using an example with my code.

    <?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    global $wpdb;
    $prefix = $wpdb->prefix;
    echo "<pre>";
    $umeta=$prefix.'usermeta';
    $u=$prefix.'users';

    $sql ="select ID, display_name from $u where ID=1";
    $result = $wpdb->get_results($sql, ARRAY_A);
    print_r($result);
    foreach($result as $value){
    print_r($value);
    $user_id = $value['ID'];
    $my_user = new WP_User($user_id );
    $my_user->remove_role( "Ladder" );
    print_r($my_user);
    }
    ?>

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.