Want to get the role of current user in WordPress? I’m assuming you know what hooks of WordPress you want to use. So skipping that part, it’s pretty easy to get the current role of the user.
1 2 3 4 5 6 7 | $current_user = wp_get_current_user(); if ( ! ($current_user instanceof WP_User) ) { return; } //$roles is an array $roles = $current_user->roles; |
You can use current_user_can to look for specific capabilities, if you just want to check whether or not a user has a specific permission versus whether or not they’re in the role. For example:
1 2 3 | if (current_user_can('delete_posts')) { //display the delete posts button. } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.