How to get the author role in WordPress
In WordPress, there are six basics user capabilities and roles.
When the blog has many authors(users) it becomes a challenge to reveal the roles of a user.
Although, it is possible to get an author role in a very simple way.
WordPress can provide user’s information by a get_userdata function.
It is a function that has an object which is able to expose the author’s role.
This post shows two techniques to get the role.
The first technique uses the get_userdata function.
The second technique uses the get_queried_object_id.
Let’s dive in now.
Snippet to get author role
You need to specify the id of the author as the first step before anything.
Retrieve the author’s id by adopting the get_the_author_meta function.
$authorID = get_the_author_meta('ID');
get_userdata can return the user data if the id has been provided.
It passes the id to return the current user information.
$theAuthorDataRoles = get_userdata($authorID);
This returns the object of WP_USER class.
The WP_USER object calls roles in which the user roles are accessed.
$theRolesAuthor = $theAuthorDataRoles -> roles;
The next part is to display the lists of roles the author possesses.
Display author role
To display the roles use the echo keyword and implode function.
<?php
echo 'author roles'.implode(',',$theRolesAuthor).'\n';
?>
The statement $theRolesAuthor = $theAuthorDataRoles -> roles create the array of roles elements.
The elements are separated by a comma.
Therefore, implode function removes those commas.
The output is all author capabilities.
For example, if the author is an administrator, author, and contributor.
The final result is
administrator
author
contributor
Let’s put the complete snippet to get and display the author roles.
<?php
$authorID = get_the_author_meta('ID');
$theAuthorDataRoles = get_userdata($authorID);
$theRolesAuthor = $theAuthorDataRoles -> roles;
echo 'author roles'.implode(',',$theRolesAuthor).'\n';
?>
Forget get_the_author_meta, a function get_queried_object_id can provide the current user id.
$thePostID = get_queried_object_id();
The get_querird_object_id returns the ID of the current post.
This time post id will be used to fetch the current post author ID.
The function to load the id of the author for this case is get_post_field.
get_post_field needs to pass two arguments to provide the required id.
- Field value ‘post_author’
- Post id.
Like this.
$authorID = get_post_field ('post_author',$thePostID);
The rest part involves the usage of the get_userdata technique.
Check the role for dynamic text display
Do you want to show messages depending on the user role?
This how you should do it.
Pass the array created by $theRolesAuthor -> theAuthorDataRoles to in_array function.
in_array is a PHP function that checks if the provided value exists in an array.
It takes three parameters.
The first parameter is the value to be checked.
The second parameter is an array containing various values
The last element is a boolean element, check here to know more about this element.
Here, you will pass six basic WordPress Roles and Capabilities.
<?php
$authorID = get_the_author_meta('ID');
$theAuthorDataRoles = get_userdata($authorID);
$theRolesAuthor = $theAuthorDataRoles -> roles;
if(in_array(‘administrator’,$theRolesAuthor,true)){
echo 'hey! Welcome Administrator';
}
else if(in_array(‘author’,$theRolesAuthor,true)){
echo 'hey Author! You are allowed to create posts';
}
else if(in_array(‘administrator’,$theRolesAuthor,true)){
echo 'hey! Welcome Administrator';
}
else if(in_array(‘contributor’,$theRolesAuthor,true)){
echo 'hey! Welcome Mr Contributor';
}
if(in_array(‘subscriber’,$theRolesAuthor,true)){
echo 'hey lovely Subcriber! Edit your profile anytime';
}
?>
Display author role on the author page
The author roles can be displayed on a page by using WP_USER class.
The process to get the user capability is done by creating the function in the function.php
The function will be called directly to the author page or any WordPress page.
function showUserRole($userId){
$theAuthor = new WP_USER($userId);
$theRole = '';
$rolesArray = $theAuthor -> roles;
for($i=0;$i<count($rolesArray);$i++){
$theRole= $theRole + $rolesArray[$i].'\n';
}
return $theRole;
}
As it was said, the showUserRole function is called directly to a page.
Open the author page file and move to the loop section.
The function is written in the loop to get the current post data brought by the_posts function.
This makes it easy to retrieve the id of the post’s author.
The first step is to retrieve the user id which is Author.
$authorID = get_the_author_meta('ID');
And pass the retrieved author id to the showUserRole function.
echo showUserRole($authorID);
The function is echoed because it returns the role string.
Now it becomes simple to display the author roles to other pages like post
On other pages like a single.php posts are retrieved in the loop.
The method used in this section can shows the author’s roles on the post as well.
Conclusion
Whenever you want to get the author role the current user id is the first required data.
The id guides get_userdata to return the appropriate object that holds the roles.
Other WordPress author features you may like.