Utilising User Roles For Flawless Custom WordPress Plugin Development

0
4782

WordPress Web/Plugin Development turns up impressively when implemented as a team. With a group of proficient WordPress coders working towards creating a website/plugin, the chances for getting a flawless final product increase. Roles have always been considered as integral units of WordPress web development/custom WordPress plugin development processes. With easy set up of custom user types, it becomes feasible for the WordPress programmer to take full advantage of WordPress built-in functionalities. Through this post, I’ll be highlighting the importance of utilizing user roles while developing a WordPress plugin/extension.

Before getting into the concept of utilizing user roles during WordPress Web development, it’s vital for you to understand the basic difference between Roles and Capabilities. Well, Capabilities are the activities that a user can undertake. A good example of user capability is changing the look of the website using the Appearances section in the back-end panel. On the other hand, Roles refer to the set of capabilities that a user possesses. Like for example, a user may have a set of capabilities such as editing a post, adding a post and so on. Such a WordPress user who’s capable of performing a large number of activities can be assigned the role of an “Admin”. This is simply because “Admin” is someone who’s got the privilege of managing the entire website.

What’s the default “Roles-Capabilities” Setup for WordPress?
The default setup for WordPress includes a total of 6 roles and around 57 capabilities. Each role has a separate combination of capabilities and the same is endowed to the users who’ve the rights and privileges. It’s recommended not to modify this default setup. However, making any additions can always work in your favor.

Adding new Roles and Capabilities is always beneficial
Roles and Capabilities are two sides of the same coin. You can always start off with creating a set of new capabilities, which are held by the website administrator. For example, if you own a business website then you’d surely have a marketing team. It’s obvious that the marketing team doesn’t need access to the posts published on the website but definitely wants access to advertising statistics, trending search topics etc. Hence, as a WordPress programmer it will be beneficial to assign specific capabilities and roles to the marketing team. They shouldn’t be assigned the role of a website content manager because adding and editing blogs/articles, purely doesn’t fall under their job function.

Functions used for managing Roles and Capabilities in WordPress
In order to be able to manage the roles and capabilities in WordPress web/plugin development, all you need is the below mentioned clear-cut functions:
add_role()- This is a function that enables you to add a custom role to a user
remove_role()– This is a function that allows you to remove/delete an existing custom role
add_cap()– This is a function that aids you in adding a custom capability to an existing role
remove_cap()– This is a function that enables you to remove/delete a capability from an existing role
get_role()- Including this function in your code allows you to fetch complete information about a role as well as its associated capabilities.

Functions used for checking Roles and Capabilities
Once you’re done with adding new roles and capabilities, it’s vital to check for the same so as to make sure that only users with the right capabilities have been allotted the specific roles. Below, I’ve jotted down a few important functions that are utilized for checking different aspects related to roles and capabilities:

1. if( current_user_can( 'any_capability' ) ) {
// The user has the capability
}
else {
// The user does not have the capability
}

The above function allows you to check whether or not the currently logged-in user has a specific capability.

2. if( user_can( 48, 'any_capability' ) ) {
// The user with the ID of 48 has the capability
}
else {
// The user with the ID of 48 does not have the capability
}

The above function enables you to check whether or not an arbitrary WordPress user has a specific capability.

In addition to the above mentioned functions; referring to my previous example of a business organization(comprising of a Marketing team), if I want to check whether the currently logged-in user is from a marketing team or not, I’ll simply use the below function:

if( current_user_can( 'marketing' ) ) {
// The current user is a marketing person
}
else {
// The current user is not a marketing person

Conclusion
User roles and capabilities are considered effective tools for undertaking hassle-free and less time-consuming custom WordPress plugin development. You may use them under situations wherein you require the implementation of user-based functions. Assigning roles and capabilities allows you to manage the plugin/website and backtrack any problems that tend to creep in, while the WordPress development project is on. Thanks a ton for taking out time to read my post. I’d appreciate your feedback/comments and suggestions.

LEAVE A REPLY

Please enter your comment!
Please enter your name here