Search code examples
phpwordpressuser-roles

Wordpress: check for custom user role in frontend


I simply want to display some message when a user has a custom user role. It works with native user roles, but not with custom ones. Here is my code:

<?php
$user_ID = get_current_user_ID();
$user = new WP_User( $user_ID );
$current_user = $user->roles[0];

if( $current_user == 'client' ){
   echo 'hello client';
} else {
   // do nothing
}
?>

Any help is welcome, thanks!


Solution

  • You can try this :

    if ( is_user_logged_in() )
    {
        global $current_user;
        $user_role = $current_user->roles[0];
    
        if($user_role == 'client')
        {
        // do something
        }
    }
    

    is_user_logged_in() is used to check whether user is logged in or not