I am currently using the Members plugin to create custom user roles for an Intranet site. They are
- Chartered Member
- Board Member
I have a query loop that outputs all the users on the site without problem.
What I don’t know how to do is to get each user’s role based on their object ID so that I can display “Chartered Member” or “Board Member” for each outputted user in the query loop.
I tried writing a custom function but I’ve had no success in actually getting something output. Has anyone accomplished this before?
Referenced Plugin: Members – Membership & User Role Editor Plugin – WordPress plugin | WordPress.org English (Canada)
I think its by MemberPress.
I was able to solve this by using the following custom function.
//This function below will identify the user's role based on the user's ID and return it.
function get_user_role($user_id)
{
$user = get_user_by("id", $user_id);
if ($user) {
$user_roles = $user->roles;
//return $user_roles;
if (!empty($user_roles)) {
if (in_array("board_member", $user_roles)) {
return "Board Member";
} elseif (
!in_array("board_member", $user_roles) &&
!in_array("administrator", $user_roles)
) {
return "Chartered Member";
} else {
return ucfirst($user_roles[0]);
}
}
}
}
Then I simply used the following echo statement and it worked fine.
{echo:get_user_role({wp_user_id})}
Finally, remember to do the code checks and signatures, and also to add the appropriate bricks generated functions to the child function.php file to allow any custom functions to work.