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 Professional Support for Your Website Problems?

Whether you're facing website issues or struggling with code implementation, our team is here to assist. Hire us to get your website back on track.

Hire Us