How to add a User Role for ‘Client’s to WordPress


The Client Role Plugin is a simple WordPress plugin that when activated creates a new user role called ‘Client’. This plugin is useful for website owners who want to provide limited access to clients or collaborators without giving them full administrative access.

This role will have the same capabilities as the ‘Administrator’ role, However, the plugin also revokes certain capabilities from the ‘Client’ role using the remove_cap() function. Specifically, it removes the following capabilities from the role:

  • update_core
  • activate_plugins
  • install_plugins
  • update_plugin
  • edit_plugins
  • edit_themes
  • export
  • import
  • create_users
  • manage_options
  • switch_themes

By removing these capabilities, the ‘Client’ role will have a more limited set of permissions that are appropriate for clients or collaborators who don’t need full administrative access. For example, clients will be able to create and edit their own content, but they won’t be able to install or update plugins or themes, modify site settings, or perform other administrative tasks.

Installation Instructions

  1. Create a new folder in the wp-content/plugins/ directory and name it whatever you like (e.g. client-role-plugin).
  2. Create a new file inside the folder and name it client-role-plugin.php.
  3. Paste the following code into the file:
<?php
/**
* Plugin Name: Client Role Plugin
* Plugin URI: https://gist.github.com/philhoyt/54a4a4e5a48d8cb0beb20edf1ba1ae51
* Description: Adds a 'Client' user role and revokes certain capabilities from it.
* Version: 1.0.0
* Author: Your Name
* Author URI: https://philhoyt.com/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
defined( 'ABSPATH' ) || exit; // Prevent direct access to this file.
/**
* Main plugin class.
*/
class Client_Role_Plugin {
/**
* Initializes the plugin.
*/
public function __construct() {
add_action( 'init', array( $this, 'add_client_role' ) );
add_action( 'init', array( $this, 'revoke_client_capabilities' ), 10 );
}
/**
* Adds a 'Client' user role.
*/
public function add_client_role() {
global $wp_roles;
if ( ! isset( $wp_roles ) ) {
$wp_roles = new WP_Roles();
}
// Adding a 'new_role' with all admin caps.
$capabilities = get_role( 'administrator' )->capabilities;
$wp_roles->add_role( 'client', 'Client', $capabilities );
}
/**
* Revokes capabilities for the 'Client' user role.
*/
public function revoke_client_capabilities() {
$caps_to_remove = array(
'update_core',
'activate_plugins',
'install_plugins',
'update_plugin',
'edit_plugins',
'edit_themes',
'export',
'import',
'create_users',
'manage_options',
'switch_themes',
);
$custom_role = get_role( 'client' );
foreach ( $caps_to_remove as $cap ) {
$custom_role->remove_cap( $cap );
}
}
}
new Client_Role_Plugin();
  1. Save the file and activate the plugin from the WordPress dashboard.
  2. The ‘Client’ user role will be added automatically, and the capabilities listed in the revoke_client_capabilities() function will be revoked from the role.

Note: You can modify the capabilities to revoke or add more capabilities to the ‘Client’ user role by editing the $caps_to_remove array or the $capabilities variable in the add_client_role() function.


By:

Published:

Category:

Tags:


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.