-3
function user_role_parameter($user_id){
    global $connection;
    $query = "SELECT * FROM  users WHERE user_id = $user_id ";
    $the_user_id = mysqli_query($connection,$query);
    while($row = mysqli_fetch_assoc($the_user_id)){
        $user_id = $row['user_id'];
        $user_role = $row['user_role'];
    }
    if($user_id > 0) {
       return $user_role;
    } else {
        return "";
    }
}
2
  • 2
    What is your question? Please take time to format your code and explain your question better, it will help you get a better response. Jun 28, 2019 at 13:10
  • You should not be using mysqli_query() etc. directly in WordPress. There are APIs for getting this sort of thing. You should also check the database that you're querying. There is no user_role column. Jun 28, 2019 at 13:37

1 Answer 1

1

You should use WordPress functions to get the user role by user id. Please try code given below:

function user_role_parameter($user_id)
{
    $user_meta = get_userdata($user_id);

    if ( !empty($user_meta) ) {
        $user_roles = $user_meta->roles;
        $user_role = '';
        foreach( $user_roles as $role ){
            $user_role .= $role . ', ';
        }
        return $user_role;
    } else {
        return "";
    }
}

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