How to programmatically add a user to a role?

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

Related posts

Leave a Reply

2 comments

  1. 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);
        }
        ?>