How to Add or Remove Capabilities in User Roles in WordPress

User role management is one of the most powerful features of WordPress, allowing administrators to define what capabilities each user has on the website. In this article, we will discuss how to add or remove capabilities in user roles in WordPress, allowing you to customize the user experience according to the needs of your site.

Understanding User Roles in WordPress

WordPress comes with a user management system that includes several predefined roles, each with a specific set of capabilities. These roles are:

  • Administrator: Total control over the site.
  • Publisher: You can publish and manage publications, including those of others.
  • Author: You can publish and manage your own publications.
  • Collaborator: You can write and manage your own publications, but not publish them.
  • Subscriber: You can manage your user profile.

However, it is sometimes necessary to customize these roles to better adapt to the specific functions you want to provide to certain users.

Adding Capabilities to a User Role

To add capabilities to a user role, you can use a plugin or custom code. Here's how to do it with code.

Use of Custom Code

If you prefer not to use a plugin, you can add capabilities via the file functions.php of your theme. Use the following sample code to add a new capability to an existing role:

function add_capability() {
    $rol = get_role('editor'); // Change 'editor' to the desired role
    $rol->add_cap('edit_pages'); // Change 'edit_pages' to the capability you want to add
}
add_action('init', 'add_capability');

This code snippet adds the ability to edit pages to the editor role. You can change 'editor' and 'edit_pages' to the role and capability you need.

How to Remove Capabilities from a User Role

Removing capabilities is also a simple process and can be done in a similar way to adding capabilities.

How to Add or Remove Capabilities in User Roles in WordPress
Download our web maintenance guide Free of charge!
Free guide for freelancers and small businesses that want to avoid surprises and improve their web performance.
Here's how to do it.

Use of Custom Code

To remove a capability from a user role, you can use the following code in the file functions.php:

function delete_capability() {
    $rol = get_role('author'); // Change 'author' to the role you want to remove a capability from
    $rol->remove_cap('publish_publications'); // Change 'publish_publications' to the capability you want to remove
}
add_action('init', 'remove_cap');

In this example, we are eliminating the capability of publish publications of the author role. Remember to customize it according to your needs.

Plugins to Manage User Capabilities

If you prefer not to touch the code, there are several plugins that facilitate the management of user capabilities in WordPress. Some of the most popular ones are:

  • User Role Editor: Allows adding, deleting and editing user role capabilities in an intuitive way.
  • Members: Provides complete control over user roles and permissions.
  • Capability Manager Enhanced: Allows you to manage user roles and capabilities with ease.

These plugins are especially useful if you manage a site with many users and want to avoid technical complications.

Practical Examples and Recommendations

It is advisable to test in a development environment before applying changes to a production site. This way, you can make sure that the modifications do not affect the functionality of the site.

Internship Recommendation

  • Make backups before making significant changes.
  • Document any changes you make to roles and capabilities for future reference.
  • Use custom capability names to avoid conflicts with plugins or themes.

Modifying roles and capabilities can be a very useful process to adapt your site to your team's needs and improve user management. With these tools and examples, you will be better prepared to make adjustments according to your specific requirements.

Conclusion

Managing user roles and their capabilities is essential for any WordPress administrator. Whether you choose to do it through code or through plugins, having fine-grained control over who can do what on your site is crucial to maintaining security and organization. Customize roles according to your team's needs and improve efficiency in managing your website.