WordPress   

How to check if the currently logged-in user role matches the given user role?

This small function can be used to match any given user role with the currently logged-in user role in WordPress. For example, if you want to test if the currently logged-in user is a Customer, Subscriber, etc.

function isCurrentUserRole($role){
    $user = wp_get_current_user();
    if ( in_array( $role, (array) $user->roles ) ) {
        return true;
    }
    else{
        return false;
    }
}

Now just pass any user role to this function as a parameter and it will give you the results as true and false. In the following example, I will test if the current user role is equal to the customer or not.

if(isCurrentUserRole('customer')){
    echo 'current user is a customer';
}
else{
    echo 'current user is not a customer'
}
Need a helping hand in fixing your website issues?

If you are facing any problems in implementing these code snippets and tutorials, you can hire us to fix your website issues.

Hire Us