Wordpress: check for custom user role in frontend - php

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!

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

The examples above are problematic if the User has more than one Role set, as the code only checks the first Role the user has set.
Ideally, you should be checking if any of the User's Roles match the custom role:
if ( is_user_logged_in() ) {
global $current_user;
if( in_array('YOUR_CUSTOM_ROLE', $current_user->roles) ) {
echo 'User has Custom Role';
} else {
echo 'User does not have Custom Role';
}
}

Related

php redirect with custom user roles set and working but redirect no

I have created code to add custom user rules. It works. However when I try to tie in the code for redirecting an already logged in user based on one test role, then it doesn't work. What am I missing in this in order to have it redirect, based on a user's role. Here is the code:
add_action('init', 'cloneRoleCompany');
function cloneRoleCompany(){
global $wp_roles;
if ( ! isset( $wp_roles ) )
$wp_roles = new WP_Roles();
$adm = $wp_roles->get_role('subscriber');
//Adding a 'new_role' with all admin caps
$wp_roles->add_role('company-admin', 'Company Admin', $adm->capabilities);
$wp_roles->add_role('company', 'Company', $adm->capabilities);
$wp_roles->add_role('user', 'User', $adm->capabilities);
}
function redirect_from_front_page() {
$redirect_url = '/user-dashboard';
$expected_role = 'user';
if( is_front_page() ) {
return;
}
if( is_user_logged_in() ) {
return;
}
$user = wp_get_current_user();
$roles = $user->roles;
if ( in_array( $expected_role, $roles ) ) {
wp_safe_redirect( $redirect_url );
exit;
}
}
You have condition is_user_logged_in() which prevents to get into the redirect part of code, only logged in user have a role.
if( is_user_logged_in() ) {
return; // when user is logged in this is last instruction
}
// code below will not get executed
$user = wp_get_current_user();
$roles = $user->roles;

Wordpress - display shortcode for user certain user role

I am trying to display a shortcode of a specific user role. The user role is customer.
global $current_user;
get_currentuserinfo();
if ( user_can( $current_user, "customer" ) ){
echo do_shortcode('[gravityform id="2"]');
}
You can use the following code:
$user = wp_get_current_user();
if ( in_array( 'shop_manager', (array) $user->roles ) ) { // user role condition
echo do_shortcode('[gravityform id="2"]');
}

How to redirect a user off a page by user role?

A lot of these guides are 'redirect after login' I have mulitple user types with different levels of access, I'd like to write some code which if a 'Subscriber' role goes onto the page (Which they do not have access it'll redirect them.
Here's one of the many code snippets I've tried, but can't get it to work! Help :)
function consultant_page_redirect() {
$current_user = wp_get_current_user();
$role_name = $current_user->roles[0];
if (('subscriber' === $role_name ) &&( is_single( array(2864, 2630)) ) ) {
wp_redirect('/trial-user-page/');
}
}
function consultant_page_redirect( $user_login, $user ) {
$role_name = $user->roles[0];
if (('subscriber' === $role_name ) &&( is_single( array(2864, 2630)) ) ) {
wp_redirect('/trial-user-page/');
}
}
add_action('wp_login', 'consultant_page_redirect', 10, 2);
Add this snippet code on child theme functions.php

If logged-in user's username is in a list, do this, else do that

I'm trying to display some content only for the following usernames: Admin, Jake, Shadow, Kid.
My current code is something like this:
add_action( 'wp_enqueue_scripts', 'load_script' );
function load_script(){
if( is_user_logged_in() ){
// Display content only for the following usernames: Admin, Jake, Shadow, Kid.
} else {
// User is not logged in or is logged in but not in the list.
}
}
Thanks in advance!
you could use following code
function load_script(){
if( is_user_logged_in() ){
// Display content only for the following usernames: Admin, Jake, Shadow, Kid.
$user = wp_get_current_user(); // to get currently logged in user
if($user && isset($user->user_login) && in_array($user->user_login, array('Admin', 'Jake', 'Shadow', 'Kid') ) ) { // check if logged in user is in list
// do stuff
} else {
// User is logged in but not in the list.
}
} else {
// User is not logged in.
}
}

Get user role by ID WordPress

I need to somehow check someone's role with only their id.
I have found the current_user_can() check. But this only works for people that are logged in. How would I check for this if that user isn't the current user? I am using a phone order system but that uses the admin/specific account to order for other people.
To check if a user has a specific role, you have to get a list of their roles and see if the role is listed there.
Example function:
function user_has_role($user_id, $role_name)
{
$user_meta = get_userdata($user_id);
$user_roles = $user_meta->roles;
return in_array($role_name, $user_roles);
}
Example usage:
$user_is_subscriber = user_has_role(get_current_user_id(), 'subscriber');
The info you need to know before you proceed:
You can't get user role by ID directly.
You can get all the roles a user is assigned to.
Let's get all the roles and check if the role you're interested in is there or now.
<?php
// Get the user object.
$user = get_userdata( $user_id );
// Get all the user roles as an array.
$user_roles = $user->roles;
// Check if the role you're interested in, is present in the array.
if ( in_array( 'subscriber', $user_roles, true ) ) {
// Do something.
echo 'YES, User is a subscriber';
}
hello try this optimal code
if (get_userdata($post->post_author)->roles[0] == 'your_specified_role'){
echo 'role exist';
}else{
echo 'role does not exist';
}
$user_meta = get_userdata($user_id);
$user_roles = $user_meta->roles;
if ( in_array( 'administrator', $user_roles, true ) ) {
//echo User is a administrator';
}

Categories