WordPress: Adding user-role
- 3. WHAT AREWE GOING TO DO
• Howto add a new userrole
• Howto removea user role
• Howto assign existing capabilities to a userrole
• Howto assign custom capabilities to a userrole
• Howto troubleshoot user role and capabilities
Adding User RoleWordPress
- 4. WHAT IS USER ROLE?
A Role defines a set of tasks a user assigned the role is allowed to perform.
For example:
• Super Admin role –controls everything within a WP Network.
• Administrator role –controls a singlesite.
Adding User RoleWordPress
- 5. WORDPRESS DEFAULT USER ROLES (6)
1. Subscriber –can only manage own profile
2. Contributor –can write and manage own posts, can’t publish
3. Author – can publish and manage own posts
4. Editor –can publish and manage posts, including otherusers
5. Administrator – admin of a single site
6. Super Admin –admin of a WP Network
Adding User RoleWordPress
- 7. PREREQUISITES (OPTIONAL)
• define( 'WP_DEBUG', true );
• Install “UserSwitching” plugin by JohnBlackbourn
• Test in a minimal theme / child theme or,in a dummy plugin
Adding User RoleWordPress
- 10. SYNTAX
<?php add_role( $role_name, $display_name, $capabilities ); ?>
$role_name *
(string)What we(developers) will call the role with
$display_name *
(string)How others (general users) will know the role
$capabilities
(array)Assign the capabilities to the role in an array
Adding User RoleWordPress
- 11. function wpressians_create_user_roles() {
$supplier = add_role(
'supplier', //for multiple words use underscores: the_man
__( 'Supplier', 'text-domain' ),
array(
'read' => true,
'edit_posts' => true,
'edit_others_posts' => true
));
}
ON THEME
add_action( 'after_switch_theme', 'wpressians_create_user_roles' );
ON PLUGINS
register_activation_hook( __FILE__, 'wpressians_create_user_roles' );
Adding User RoleWordPress
- 12. WHATJUSTHAPPENED TOTHE DATABASE
NOTE
wp_ is the table prefix. If thetable prefix was changed, then both thenames will be changedlike
$mypreifx_options and $myprefix_user_roles.
wp_options
wp_user_roles value stored... ...
... ... ... ... ...
... ... ...
option_name option_value
Adding User RoleWordPress
- 13. TIPS
To understand what value has beenstored in the field:
• Copy the values of wp_user_roles
• Browse: http://jsfiddle.net
• Paste them into the JavaScripts block
• Click the button
A readable format of PHP serialized data is ready
Adding User RoleWordPress
- 14. a: 1: {
s: 8: "supplier";
a: 2: {
s: 4: "name";
s: 8: "Supplier";
s: 12: "capabilities";
a: 3: {
s: 4: "read";
b: 1;
s: 10: "edit_posts“;
b: 1;
s: 17: "edit_others_posts";
b: 1;
}
}
}
PHP ARRAYSERIALIZEDDATA
Adding User RoleWordPress
- 15. TEST THE USER ROLE
In /wp-admin, go to Users» Add New
• Username: someone
• Email: someone@example.com
• Password (twice)
• Role: Supplier
Nowusing the UserSwitching plugin Switch to “Someone” user(orlog out and log in
again with “Somone”s credentials)
Adding User RoleWordPress
- 18. What if we makechange[s] to ourfunction?
function wpressians_create_user_roles() {
$supplier = add_role(
'supplier',
__( 'Supplier', 'text-domain' ),
array(
'read' => true,
'edit_posts' => true,
'edit_others_posts' => false
));
}
Unfortunately,
it won’tchange anything.
Adding User RoleWordPress
- 19. BECAUSE
Userrole can becreated once, when:
• the theme is activated, or
• the plugin is activated
And only once.
It CAN’TOVERWRITEthe existing value.
Adding User RoleWordPress
- 21. function wpressians_create_user_roles() {
$supplier = add_role(
'supplier',
__( 'Supplier', 'text-domain' ),
array(
'read' => true,
'edit_posts' => true,
'edit_others_posts' => false
));
remove_role( 'supplier' ); //it will remove the user role
}
ASSUMING WE ARE WORKING ON A THEME
add_action( 'after_switch_theme', 'wpressians_create_user_roles' );
Adding User RoleWordPress
- 22. function wpressians_create_user_roles() {
$supplier = add_role(
'supplier',
__( 'Supplier', 'text-domain' ),
array(
'read' => true,
'edit_posts' => true,
'edit_others_posts' => false
));
//remove_role( 'supplier' ); //we commented out the removal line
}
ASSUMING WE ARE WORKING ON A THEME
add_action( 'after_switch_theme', 'wpressians_create_user_roles' );
Adding User RoleWordPress
- 23. a: 1: {
s: 8: "supplier";
a: 2: {
s: 4: "name";
s: 8: "Supplier";
s: 12: "capabilities";
a: 3: {
s: 4: "read";
b: 1;
s: 10: "edit_posts";
b: 1;
s: 17: "edit_others_posts";
b: 0;
}
}
}
PHP ARRAYSERIALIZEDDATA,NOW
Adding User RoleWordPress
- 25. function wpressians_create_user_roles() {
$supplier = add_role(
'supplier',
__( 'Supplier', 'text-domain' ),
array(
'read' => true,
'edit_posts' => true,
'edit_others_posts' => false
'view_stats' => true //assigning custom cap.
));
//remove_role( 'supplier' ); //we commented out the removal line
}
Dead simple, nah!
ASSUMING WE ARE WORKING ON A THEME
add_action( 'after_switch_theme', 'wpressians_create_user_roles' );
Adding User RoleWordPress
- 26. function wpressians_create_user_roles() {
$supplier = add_role(
'supplier',
__( 'Supplier', 'text-domain' ),
array(
'read' => true,
'edit_posts' => true,
'edit_others_posts' => false
));
remove_role( 'supplier' ); //it will remove the user role
}
WHY THEREIS A
$variable
THERE?
Adding User RoleWordPress
- 27. function wpressians_create_user_roles() {
$supplier = add_role(
'supplier',
__( 'Supplier', 'text-domain' ),
array(
'read' => true,
'edit_posts' => true,
'edit_others_posts' => false,
'view_stats' => true //our custom cap.
));
$administrator = get_role( 'administrator' );
if( null !== $supplier ) {
$administrator->add_cap( 'view_stats' );
}
}
Because, we want ourAdmin should also be able to seethe Site Stats.
ASSUMING WE ARE WORKING ON A THEME
add_action( 'after_switch_theme', 'wpressians_create_user_roles' );
Adding User RoleWordPress
- 28. HOW CAN I USE
WHAT IJUSTCREATED?
?
Adding User RoleWordPress
- 29. add_action( 'admin_menu', 'wpressians_register_stats_page');
function wpressians_register_stats_page() {
add_menu_page(
'Statistics Page', //page title
'Statistics Page', //menu title
'view_stats', //capability
'statistics-page', //menu slug
'wpressians_stats_page_callback' //callback function
);
}
function wpressians_stats_page_callback() {
if( ! current_user_can( 'view_stats' ) ) {
die( 'You are not allowed to view this page' );
}
echo "This is the site statistics page";
}
Adding User RoleWordPress
- 30. WHOLE THINGIN BRIEF
Thewhole discussion in brief can be found here:
http://wordpress.stackexchange.com/a/178629/22728
Adding User RoleWordPress
- 31. PLUGINS
There aremany plugins to manage userrole and capabilities, like:
• Members
• UserRole Editor
• Capability ManagerEnhanced
• WPFront UseRole Editor
• Webmaster User Role
and so on…
Adding User RoleWordPress
- 33. COURTESY
• NinjaImage:TMNT– Martegod–DeviantArt.com
• NinjaImage 2: TMNTLeonardo– Propimol–DeviantArt.com
• NinjaImage 3: TMNTLeonardo– PixShark.com
• ShredderImage:TMNT–Nanashi–DeviantArt.com
• NinjaImage 4: Donatello–ComicAttack.net
• SplinterImage: Covens –DeviantArt.com
• Utilizing User RolesIn WordPress–DanielPataki– SmashingMagazine
• WordPressiansGroup andWordPressStackExchange
Thanks and JazakALLAH to you all.
Adding User RoleWordPress