Home WordPress How to check the role of Current User ?

How to check the role of Current User ?

by admin
0 comment 143 views

WordPress is very good in providing different types functions to check where a property of anything exits or not. From the data provided by functions, we use some sort of PHP code to get things done. I found many types bit complicated to check roles of the user. So I found a solution, to accomplish get current user details and then from that array we can check whether the role of that user exits or not.

We first use WordPress function wp-_get_current_user(); to get the users details and the use the roles object to find out the weather is roles exits or not. So by default, administrator, author, contributor, subscribers and editor roles exits. we can use any of the roles to check whether the role exits or not

$user = wp_get_current_user();
if ( in_array( 'author', (array) $user->roles ) ) {
    // do your work
}

Leave a Comment

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy