How to get current users privileges in wordpress a plugin with respect to their roles;
Eg:-
if input is editor function should return delete_others_posts, if input is author result will be delete_published_posts and so on...
Thanks in advance
Get the name of the current user role :
function get_current_user_role() {
global $current_user;
return array_shift($current_user->roles);
}
Then using get_role($role_name) function you can get an array with all his capabilities.
E.G : $capabilities = get_current_user_role();
Another solution is to use the current_user_can($capability_name) function that return true or false
For entire explanations : http://tomarea.fr/wordpress-roles-capacites-utilisateurs/
To elaborate on koma182's answer, wrap get_role() with a var_dump or print_r and you'll see the capabilites of the current user role:
function get_current_user_role() {
global $current_user;
return array_shift($current_user->roles);
}
and then where you call the function:
var_dump(get_role(get_current_user_role()));
Thanks Koma!
Related
I have a ticket support form on my site which right now has a field which returns (in the admin area) the name of the person who submitted the form.
Anyone know how I would modify this to display their user role instead? ie. Subscriber, Editor, etc.
$raised_by='';
if($ticket->type=='user'){
$user=get_userdata( $ticket->created_by );
$raised_by=$user->display_name;
}
I'm guessing it'll be something with this stuff in it...but I'm not too savy when it comes to this.
function get_user_role() {
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
return $user_role;
}
Please change last line of your code to this:
$raised_by=ucwords($user->roles[0]);
So that your current code which display First Name i.e.
$raised_by='';
if($ticket->type=='user'){
$user=get_userdata( $ticket->created_by );
$raised_by=$user->display_name;
}
Above code will become:
$raised_by='';
if($ticket->type=='user'){
$user=get_userdata( $ticket->created_by );
$raised_by=ucwords($user->roles[0]);
}
Update: To remove underscore with space your code may become as:
$raised_by='';
if($ticket->type=='user'){
$user=get_userdata( $ticket->created_by );
$raised_by= ucwords(str_replace("_"," ",$user->roles[0]));
}
You may notice, I have added ucwords function of PHP also, it is to make sure , roles on the screen look good, i.e. admin will be shown as Admin etc.
Also you may notice roles[0], 0 means that data currently we have there is as an array. So we are picking the first user roles from all the roles assigned to the user. I am sure it will be sufficient for your needs.
Let me know if this solves your issue or you still need any help. You can post in comments. Or Update your question.
You could use this line of code.
$raised_by='';
if($ticket->type == 'user'){
$user = get_userdata( $ticket->created_by );
$raised_by = implode(', ', $user_info->roles);
}
Or, if you prefer to use the get_user_role function that you've written,
slightly modify it to take the user ID as input and return the user role.
function get_user_role($user_id) {
$user_info = get_userdata($user_id);
$user_roles = $user->roles;
$user_role = array_shift($user_roles);
return $user_role;
}
You could use it like as shown below to output the user role.
$raised_by='';
if($ticket->type == 'user'){
$user = get_userdata( $ticket->created_by );
$raised_by = get_user_role($user->ID);
}
I would like to know how I can prompt a user that is already logged-in to confirm their login credentials in order to validate their identity.
I see that there are a few functions in the codex that seem to do this but I am unsure where to start or how to use them.
Any help or advice would be much appreciated.
thank you
Maybe use something like this:
https://wordpress.org/plugins/si-captcha-for-wordpress/
sometime we need a wordpress functionality to activate and deactivate user. or we need to check user has confirm the mail or not we need such kinds of value before user login for this please check below function in wordpress.
function check_user_status($user, $username, $password) {
if (in_array( 'subscriber', (array) $user->roles ) ) {
if (get_user_meta($user->ID, 'confirm_mail', true) == 1) { return $user; }
else{ return new WP_Error('Account Not Active.'); }
}
else{ return $user; }
}
add_filter('authenticate','check_user_status', 30, 3);
here we have put condition it will only validate confirm mail for subscriber user.
I am using wp_create_user function to register new user in wordpress.
This function is inserting user as a role of subscriber. I want to insert users as a author type role. How can i do this.?
Thanks in advance.
The wp_create_user() function returns the ID of the user it creates. You can then use the WP_User class like:
$new_user_id = wp_create_user($your_args_here);
$u = new WP_User( $new_user_id );
// Remove role
$u->remove_role( 'subscriber' );
// Add role
$u->add_role( 'author' );
Alternatively, you can use wp_update_user() function:
Although I'm not precisely sure how to do that, but I imagine you could work it out by looking in wp-includes/registration.php which is where that function is created.
While a user is creating a new post, how do I determine his current role?
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
$current_user = wp_get_current_user();
if ( !($current_user instanceof WP_User) )
return;
$roles = $current_user->roles; //$roles is an array
Now, you can iterate over that array to see if the user has a particular role.
Or, 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:
if (current_user_can('delete_posts')) {
//display the delete posts button.
}
This code will help you
<?php echo array_shift(wp_get_current_user()->roles); ?>
This must be an easy one but I can't find documentation for it online.
I'm trying to use the l() function in Drupal to create a dynamic link. What's the syntax?
At the moment I have:
l('Destination',"path/$user->uid/category")
which points to:
path/%2Fcategory
first of all, if you're working within a function, you'll need to get access to the global user object.
Secondly, if the user is anonymous/not logged in, the $user->uid might not be set or be 0.
lastly to prevent errors, it is common to concatenate variables together with strings
global $user;
if ($user->uid)
{
l('Destination', 'path/'.$user->uid.'/category')
}
l() is correcting your URL to path/%2Fcategory because it's trying to make a workable link from the string path//category.
Your string is path//category because $user->uid has no value. It has no value because either you haven't pulled up a user object from global $user or user_load(), or your user is anonymous.
I would suggest putting checking the value of $user before calling l(), for example:
global $user; // or $user = user_load($foo);
if ($user) {
l('Destination', 'path/'.$user->uid.'/category');
} else {
l('Destination', 'path/you-are-not-logged-in');
}
Try concatenating the strings instead.
l('Destination',"path/".$user->uid."/category")
as for the documentation, here it is: http://api.drupal.org/api/function/l/4.7
l($text,
$path,
$attributes = array(),
$query = NULL,
$fragment = NULL,
$absolute = FALSE,
$html = FALSE)
The documentation for the l() function is located at:
http://api.drupal.org/api/function/l/6
Other stuff has been said yet by others :)