Skip to content

Instantly share code, notes, and snippets.

@philhoyt
Created April 11, 2023 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philhoyt/54a4a4e5a48d8cb0beb20edf1ba1ae51 to your computer and use it in GitHub Desktop.
Save philhoyt/54a4a4e5a48d8cb0beb20edf1ba1ae51 to your computer and use it in GitHub Desktop.
WordPress Client Role Plugin - Create a user role similar to Admin and revokes specific capabilities.
<?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