Is there any way to get the explicit role that a user belongs to in my controller? This assumes using ASP.NET Membership and Role Providers. "IsInRole" doesn't work - I need to actually get the name of the role they are in.
First off, we check that the user is actually logged in. If they're not logged in, they won't have a role assigned. If the user is logged in, we use wp_get_current_user to return the WP_User object. This provides us with a stack of information about the data and we can access their user role(s) via $user->roles .
Getting all the information of current user in wordpress using the predefined function wp_get_current_user(); <? php $current_user = wp_get_current_user(); echo "Username :". $current_user->user_login; echo "Username :".
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. $user_meta=get_userdata($user_id); $user_roles=$user_meta->roles; if (in_array("subscriber", $user_roles)){} Results in a role check for subscriber.
If you want to use it in your visible property of any screen widget then you can create an action using "Fetch Data from Other Sources" which is available on screen when you right click. In this action your service side role actions will be available which you can call and assign their result to your output variable.
A user can be in more than one role so you can't get the one role that the user is in, but you can easily get the list of roles a user is in.
You can use the Roles
type to get the list of roles that the currently logged in user is in:
public ActionResult ShowUserRoles() { string[] roleNames = Roles.GetRolesForUser(); return View(roleNames); }
Or if you want to get the roles for an arbitrary user you can pass in the username when you call Roles.GetRolesForUser()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With