Skip to content

Instantly share code, notes, and snippets.

@igorbenic
Last active June 21, 2019 15:39
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igorbenic/951d27a58c92f1d55a890b60b279e094 to your computer and use it in GitHub Desktop.
Save igorbenic/951d27a58c92f1d55a890b60b279e094 to your computer and use it in GitHub Desktop.
How to Manage WordPress User Roles & Capabilities with Code | http://www.ibenic.com/how-to-manage-wordpress-user-roles-capabilities-with-code
<?php
add_role(
'new_user_role',
__( 'New User Role', 'yourtextdomain' ),
array(
'read' => true,
'edit_posts' => true,
// Various Capabilities
));
<?php
/**
* Checking Capability
*/
// Useful when you are not doing something for the current user, but for others
// Example: a CRON JOB
$user = 1; // It can be an ID or WP_User Object
$capability = 'edit_posts';
if( user_can( $user, $capability ) ) {
// Do Something
}
// Useful when you are checking something for the current user
if( current_user_can( 'edit_posts' ) } {
// Do Something
}
// Useful when working on a multisite
if( current_user_can_for_blog( $blog_id, 'edit_posts' ) ) {
// Do Something
}
// Useful when checking the capability for the author of the provided post
$post = 49; //It can also be a WP_Post object
if( author_can( $post, 'edit_posts' ) ) {
// Do Something
}
<?php
add_action( 'add_user_role', 'hook_user_add_role', 20, 2 );
/**
* A user received a new role
* @param number $user_id
* @param string $role
**/
function hook_user_add_role( $user_id, $role ) {
if( $role == 'administrator' ) {
// This user has been added to administrator role, do something
}
}
<?php
add_action( 'remove_user_role', 'hook_user_remove_role', 20, 2 );
/**
* A role removed from the user
* @param number $user_id
* @param string $role
**/
function hook_user_remove_role( $user_id, $role ) {
if( $role == 'administrator' ) {
// This user is not an administrator anymore, do something
}
}
<?php
add_action( 'set_user_role', 'hook_user_set_role', 20, 3 );
/**
* A role has been set, others were removed
* @param number $user_id
* @param string $role
* @param array $old_roles
**/
function hook_user_set_role( $user_id, $role, $old_roles ) {
if( $role == 'administrator' && in_array( 'subscriber', $old_roles ) ) {
// This user is now an administrator and it was a subscriber before
// let's do something
}
}
<?php
/**
* Removing the role
*/
remove_role( 'administrator' );
<?php
/**
* Updating the role
*/
$administrator_role = get_role( 'administrator' );
// Adding a new capability to role
$administrator_role->add_cap( 'custom_capability' );
// Remove a capability from role
$administrator_role->remove_cap( 'custom_capability' );
<?php
/**
* Capabilities on User
*/
$user_id = 1;
// Getting the WP_User object
$user = get_userdata( $user_id );
// The user exists
if( $user && $user->exists() ) {
// Add Capability
$user->add_cap( 'edit_posts' );
// Remove Capability
$user->remove_cap( 'edit_posts' );
// Remove All Capabilities from the User
// This will reset the capabilties to the User Role
$user->remove_all_caps();
}
<?php
/**
* Manage Roles for a User
*/
$user_id = 1;
// Getting the WP_User object
$user = get_userdata( $user_id );
// The user exists
if( $user && $user->exists() ) {
// Remove all the previous roles from the user and add this one
// This will also reset all the caps and set them for the new role
$user->set_role( 'administrator' );
// Remove the Role from the user
$user->remove_role( 'administrator' );
// Add a new role to the user, while retaining the previous ones
$user->add_role( 'administrator' );
}
<?php
$administrator_role = get_role( 'administrator' );
/* $administrator role will be: */
WP_Role => array(
'name' => 'administrator',
'capabilities' => array(
'switch_themes' => true,
'edit_themes' => true,
'activate_plugins' => true,
// Much more
)
)
<?php
WP_Roles => array(
'roles' => array(
'administrator' => array(
'name' => 'administrator',
'capabilities' => array(
'switch_themes' => true,
'edit_themes' => true,
'activate_plugins' => true,
// Much more
),
// Other roles
),
'role_names' => array(
'administrator' => 'Administrator',
// Other role names
)
// ...
);
@marktenney
Copy link

These have been really helpful to me! Thank you!

One quick edit: I believe line 16 of check_capaility.php needs a ) where you have a }.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment