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';
}
Related
I am trying to modify a code snippet I have been using for some time on my WordPress site. Previously, when a user with the default 'Subscriber' role logged in, the role would be changed to 'Directory User'. I had excluded the 'Administrator' role from being changed. I want to keep this behavior but I have added a new user role, 'Directory Contributor' that I don't want to be changed either. I have come up with the following code, but the 'Directory Contributor' role is being changed to 'Directory User'. How can I modify my code so that this does not occur?
function uiwc_change_role()
{
// get WP_user object
$user = wp_get_current_user();
$role = $user->roles;
// if the this is a registered user and this user is not an admin or directory contributor
if (is_user_logged_in() && !user_can($user, 'administrator', 'directory_contributor') && !in_array('directory_user', $role)) {
//set the new role to directory user
$user->set_role('directory_user');
}
}
add_action('wp_footer', 'uiwc_change_role');
You want to use array_intersect since $role is an array. user_can checks for capabilities, and not user roles, so it's probably best to leave it out of the picture here.
It also seems more sensible to hook to init unless you have some other reason not to.
function uiwc_change_role() {
// get WP_user object.
$user = wp_get_current_user();
$role = $user->roles;
// if the user is a registered user and this user is not an admin or directory contributor.
if ( is_user_logged_in() && ! array_intersect( array( 'administrator', 'directory_contributor' ), $role ) ) {
// set the new role to directory user.
$user->set_role( 'directory_user' );
}
}
add_action( 'init', 'uiwc_change_role' );
I'm trying to adapt "Completely hide products from unauthorized users in WooCommerce" answer code to also allow several custom user roles to view this products. I believe the best way to accomplish this is to expand the authorized user function to include this user roles.
This is the changes that I have tried to implement with no success. Can someone shine a light on how to proceed?
// Conditional function checking for authorized users
function is_authorized_user() {
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
$caps = $user->allcaps;
if ( ( isset($caps['edit_product']) && $caps['edit_product'] ) ||
array( 'custom_user_role1', 'custom_user_role2', $user->roles ) )
return true;
} else
return false;
}
How to make it work for an array of user roles, instead of just one? Any help is appreciated.
As you have 2 arrays to compare:
your 2 custom roles (in an array)
the current user roles (that is an array)
you can use array_intersect() php function to make it work this way:
// Conditional function checking for authorized users
function is_authorized_user(){
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
$caps = $user->allcaps;
if ( ( isset($caps['edit_product']) && $caps['edit_product'] ) ||
array_intersect( ['custom_user_role1', 'custom_user_role2'], $user->roles ) ) {
return true;
}
return false;
}
else {
return false;
}
}
It should work now for multiple user roles.
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';
}
}
We have a complicated weird system where we need to check if the wordpress administrator has logged in and by logged in i dont just mean the session but initial second when the query is sent to validate the credentials entered. I was thinking doing traditional php where I do something like the following:
if(isset($_POST['submit']) { // do something }
But that would be the incorrect way. Where exactly would i look to see this peice , i believe its wp-login.php but wasnt able to find what i was looking for.
thanks
try
global $current_user;
$user = get_userdata($current_user->ID);
$userRole = (!empty($user->roles) ? $user->roles : '');
if(in_array('administrator', $userRole)) {
// do your stuff
}
or you can try :- http://codex.wordpress.org/Function_Reference/current_user_can
You can use very neat Wordpress function to determine the user capabilities, it is called current_user_can():
<?php
if ( current_user_can('update_core') ) {
echo 'The current user is Administrator or Super administrator (in Multisite)';
}
Only Administrators of single site installations have the below list of capabilities. In Multi-site, only the Super Admin has these abilities:
update_core
update_plugins
update_themes
install_plugins
install_themes
delete_themes
edit_plugins
edit_themes
edit_users
create_users
delete_users
unfiltered_html
See here for more information.
We can add some custom action inside the filter hook login_redirect. If the form was not submitted, the $user param is a WP_Error Object.
add_filter( 'login_redirect', function( $redirect_to, $requested_redirect_to, $user )
{
if( !is_wp_error( $user ) && in_array( 'administrator', $user->roles ) )
{
# Do your thing; here just inspecting the WP_User Object
wp_die(
sprintf( '<pre>%s</pre>', print_r( $user, true ) ),
'Var dump',
array(
'response' => 500,
'back_link' => true
)
);
}
return $redirect_to;
}, 10, 3 );
BuddyPress provides the "bp_user_can_create_groups" filter to restrict a user's ability to create groups. Reference - http://wordpress.org/support/topic/restrictallow-group-creation-by-user-role?replies=3#post-4617184
and
http://etivite.com/api-hooks/buddypress/trigger/apply_filters/bp_user_can_create_groups/
How can I use this filter to restrict group creation for a particular user role in BuddyPress? Currently, only admins are allowed to create groups and I also dont want to allow every user to be able to create groups.
I have added the following code to bp-custom.php but its not working
function create_groups1( $can_create, $restricted=false ) {
// maybe we don't want to override if it's restricted?
if ( ! $restricted ){
// get the logged in user's ID
$user_ID = get_current_user_id();
// some logic to determine if the current user can create a group
$user1 = new WP_User( $user_ID );
if ( !empty( $user1->roles ) && is_array( $user1->roles ) ) {
$r=$user1->roles[0];
}
if (current_user_can('read')) {
$r="subscriber";
}
if ( $r == "subscriber" || $r == "participant" ){
// we will return this allowing them to create groups
$can_create = true;
}
}
return $can_create;
}
add_filter( 'bp_user_can_create_groups', 'create_groups1', 10, 2 );
You need to write a function and attach it to the 'bp_user_can_create_groups' hook. Inside your function determine if you want the current user to be able to create groups and return $can_create accordingly.
function bp_user_can_create_groups( $can_create, $restricted=false ){
// maybe we don't want to override if it's restricted?
if ( ! $restricted ){
// get the logged in user's ID
$user_ID = get_current_user_id();
// some logic to determine if the current user can create a group
if ( user_can_create_group( $user_ID ) ){
// we will return this allowing them to create groups
$can_create = true;
}
}
return $can_create;
}
add_filter( 'bp_user_can_create_groups', 'bp_user_can_create_groups', 10, 2 );