Last active
June 21, 2019 15:39
-
-
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
This file contains hidden or 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 | |
add_role( | |
'new_user_role', | |
__( 'New User Role', 'yourtextdomain' ), | |
array( | |
'read' => true, | |
'edit_posts' => true, | |
// Various Capabilities | |
)); |
This file contains hidden or 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 | |
/** | |
* 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 | |
} |
This file contains hidden or 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 | |
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 | |
} | |
} |
This file contains hidden or 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 | |
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 | |
} | |
} |
This file contains hidden or 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 | |
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 | |
} | |
} |
This file contains hidden or 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 | |
/** | |
* Removing the role | |
*/ | |
remove_role( 'administrator' ); |
This file contains hidden or 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 | |
/** | |
* 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' ); |
This file contains hidden or 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 | |
/** | |
* 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(); | |
} |
This file contains hidden or 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 | |
/** | |
* 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' ); | |
} |
This file contains hidden or 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 | |
$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 | |
) | |
) |
This file contains hidden or 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 | |
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 | |
) | |
// ... | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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}
.