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

Got an idea? We'll turn it into reality.

Tell us what you're building, what's broken, or what numbers need to move. We'll come back within one business day with a short plan — no long decks, no sales calls without purpose.

Response time: First reply within 1 business day on 100% of messages.
Transparent scope: Fixed proposals with weekly check-ins & live task tracking.
Ship-ready builds: Every engagement leaves behind documented, transferable work.
Long-term partners: Ongoing retainers for support, SEO, ads, and content.