• Home
  • WordPress
    • How To
    • Plugins
    • Themes
      • Genesis
      • Themify
      • Mysite myway
    • Security
    • Developer
    • Teaching
    • Conference
    • Chat
  • Blog
  • About
  • Freelance
    • Plugins
    • Teaching
    • Need help?
    • CV og ref.
  • Contact

Easy Web Design Tutorials

WordPress Tutorials and more

  • Home
  • WordPress
    • How To
    • Plugins
    • Themes
      • Genesis
      • Themify
      • Mysite myway
    • Security
    • Developer
    • Teaching
    • Conference
    • Chat
  • Blog
  • About
  • Freelance
    • Plugins
    • Teaching
    • Need help?
    • CV og ref.
  • Contact

22 June - 2015 By Paal Joachim 1 Comment
Last updated on: June 26, 2015

How to create a custom user role in WordPress.

Creating a custom user role

The simple way is using a plugin.

Plugins:
Switching user roles plugin
User Role Editor
Capability Manager Enhanced

Multiple roles per user

 

The manual way.

Something like this can be added to the child theme functions file.

/* Add a custom user role to the site
 * https://codex.wordpress.org/Roles_and_Capabilities
 * */
 $result = add_role('client', 'Client', array(
 'read' => true,
 'edit_posts' => true,
 'delete_posts' => true,
 ));
/* remove the unnecessary roles */
 remove_role('slug name of role to be removed');

NB a tip! To find the slugs of the various Roles. Right click and select Inspect Element. Then check out the Role drop down. (See the screenshot). The example seen is how to delete the user 0 by finding the slug custom-made. I then inserted the name custom-made into the remove role code, refreshed the file and commented out the code from the list.

User Role Slugs Inspect Element

User Role Slugs Inspect Element

 

Clone the administrator user role

//https://www.kvcodes.com/2015/04/wordpress-clone-user-role-to-create-new-role/
//https://nazmulahsan.me/clone-or-add-new-user-roles/
add_action('init', 'cloneUserRole');
function cloneUserRole()
{
 global $wp_roles;
 if (!isset($wp_roles))
 $wp_roles = new WP_Roles();
 $adm = $wp_roles->get_role('administrator');
 // Adding a new role with all admin caps.
 $wp_roles->add_role('client', 'Client', $adm->capabilities);
}

Modify the newly cloned user

// https://www.wpmayor.com/how-to-remove-menu-items-in-admin-depending-on-user-role/
add_action( 'admin_init', 'my_remove_menu_pages' );
function my_remove_menu_pages() {
global $user_ID;
if ( current_user_can( 'client' ) ) {
remove_menu_page( 'tools.php' ); // Tools
 remove_menu_page( 'users.php' ); // Users
 
 remove_submenu_page( 'index.php', 'update-core.php' ); // Dashboard - Updates
 remove_submenu_page( 'edit.php', 'edit-tags.php?taxonomy=post_tag' ); // Posts - Tags 
 
 remove_submenu_page( 'themes.php', 'theme-editor.php' ); // Appearance - Editor
 remove_submenu_page( 'plugins.php', 'plugin-editor.php' ); // Plugins - Editor
 }
}

 

Activate a new user role through a plugin.

I am working on a custom functions plugin I call AWP Custom Functions (AWP = https://www.facebook.com/groups/advancedwp/ as it was Devin Walker through AWP who first got me hooked on the concept of a custom functions plugin. )

To activate a new user role through a plugin add something like the following code to your main plugin file.

// On plugin activation create following user role.
 //https://codex.wordpress.org/Function_Reference/add_role
 // https://codex.wordpress.org/Function_Reference/register_activation_hook
 // FILE = path to the main plugin file inside wp-content/plugins directory. A full path will work.
 // function: rename it to plugin file name.
 function awpcustomfunctions_activate() {
 add_role( 'custom-client', 'Custom Client',
 array(
 'read' => true ) );
 }
 register_activation_hook( __FILE__, 'awpcustomfunctions_activate' );

 

These are most of the options (capabilities) available.

User role options differ from the remove_menu_page etc code used when reordering the left admin bar.

 Some are set to true. Others to false.
Only the ones set to true need to be listed.

$result = add_role('customer', 'Customer', 
array(
// Dashboard
'read' => true, // Access to Dashboard and Users -> Your Profile.
'update_core' => false, // Can NOT update core. I added a plugin for this.
'edit_posts' => true, //Access to Posts, Add New, Comments and moderating comments.
// Posts
'edit_posts' => true, //Access to Posts, Add New, Comments and moderating comments.
'create_posts' => true, // Allows user to create new posts
'delete_posts' => true, // Can delete posts.
'publish_posts' => true, // Can publish posts. Otherwise they stay in draft mode.
'delete_published_posts' => true, // Can delete published posts.
'edit_published_posts' => true, // Can edit posts.
'edit_others_posts' => true, // Can edit other users posts.
'delete_others_posts' => true, // Can delete other users posts.
// Categories, comments and users
'manage_categories' => true, // Access to managing categories.
'moderate_comments' => true, // Access to moderating comments. Edit posts also needs to be set to true.
'edit_comments' => false, // Comments are blocked out for this user.
'edit_users' => false, // Can not view other users.
// Pages
'edit_pages' => true, // Access to Pages and Add New (page).
'publish_pages' => true, // Can publish pages.
'edit_other_pages' => true, // Can edit other users pages.
'edit_published_ pages' => true, // Can edit published pages.
'delete_pages' => true, // Can delete pages.
'delete_others_pages' => true, // Can delete other users pages.
'delete_published_pages' => true, // Can delete published pages.
// Media Library
'upload_files' => true, // Access to Media Library.
// Appearance
'edit_themes_options' => true, // Access to Appearance panel options.
'switch_themes' => false, // Can not switch themes.
'delete_themes' => false, // Can NOT delete themes.
'install_themes' => false, // Can not install a new theme.
'update_themes' => false, // Can NOT update themes.
'edit_themes' => false, // Can not edit themes - through the appearance editor.
// Plugins
'activate_plugins' => true, // Access to plugins screen.
'edit_plugins' => false, // Can not edit plugins - through the appearance editor.
'install_plugins' => true, // Access to installing a new plugin.
'update_plugins' => true, // Can update plugins.
'delete_plugins' => false, // Can NOT delete plugins.
// Settings
'manage_options' => false, // Can not access Settings section.
// Tools
'import' => false, // Can not access Tools section.
)
);
/* remove the unnecessary roles 
remove_role('client');

 

 

Some good resources to check out are:
https://codex.wordpress.org/Roles_and_Capabilities
https://85ideas.com/wp-tutorials/how-to-create-custom-user-roles-in-wordpress/
https://managewp.com/create-custom-user-roles-wordpress
https://stackoverflow.com/questions/8413560/wordpress-add-custom-roles-as-well-as-remove-default-roles
https://markwilkinson.me/2014/05/allow-wordpress-editors-access-widgets-menus/
https://www.wpbeginner.com/plugins/how-to-add-or-remove-capabilities-to-user-roles-in-wordpress/

 

 

Share this:

  • Email

Categories: Developer, WordPress

Paal Joachim Romdahl

I enjoy teaching and creating tutorials. As well creating web sites.
I help people gain WordPress knowledge through my easy to follow tutorials and specialized training. Contact me for more information on how I can improve your WordPress skills and to help get your web site quickly up and running.

Comments

  1. צלם אירועים says

    23 May - 2018

    Thank you for the information

    Reply

Leave a Comment Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2021 · By Easy Web Design Tutorials · Built on the Genesis Framework · WordPress · Log in · ⇪

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.