WordPress Client Role Plugin - Create a user role similar to Admin and revokes specific capabilities.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment