Get started with 33% off your first certification using code: 33OFFNEW

Create your own user level in WordPress

4 min read
Published on 5th June 2024

WordPress is a powerful content management system that offers extensive flexibility and customization options. One of its standout features is the ability to manage user roles and permissions. By default, WordPress comes with predefined user roles like Administrator, Editor, Author, Contributor, and Subscriber, each with its own set of capabilities. You can read about each here. However, sometimes these default roles do not fit your specific needs. This guide will walk you through creating your own custom user level in WordPress with unique permissions.

Understanding WordPress User Roles and Capabilities

Before diving into creating custom user levels, it's essential to understand how WordPress handles user roles and capabilities.

  • Roles: A role is a collection of permissions. Each user is assigned a role, which defines what they can and cannot do on your WordPress site.
  • Capabilities: Capabilities are specific permissions that allow users to perform particular tasks. For example, 'edit_posts' is a capability that allows users to edit posts.

Step-by-Step Guide to Creating Custom User Levels

Step 1: Setting Up Your Environment

Ensure you have a WordPress site set up and ready. It's recommended to use a child theme or a custom plugin to add your custom user levels. For this tutorial, we'll create a custom plugin.

  1. Create a Custom Plugin:

    • Navigate to your wp-content/plugins directory.
    • Create a new folder named custom-user-roles.
    • Inside this folder, create a PHP file named custom-user-roles.php.
  2. Add Plugin Header Information: Open custom-user-roles.php and add the following code to set up the plugin:

    <?php
    /*
    Plugin Name: Custom User Roles
    Description: A plugin to add custom user roles with unique permissions.
    Version: 1.0
    Author: Your Name
    */
    

Step 2: Registering Custom User Roles

With the plugin setup, you can now define your custom user roles. Let's create a custom role called "Manager" with unique permissions.

  1. Define the Role: Add the following code to custom-user-roles.php to register the "Manager" role with specific capabilities:

    function add_custom_roles() {
        add_role(
            'manager',
            __('Manager'),
            array(
                'read' => true,
                'edit_posts' => true,
                'delete_posts' => true,
                'publish_posts' => true,
                'upload_files' => true,
                'edit_others_posts' => true,
                'create_users' => true,
                'list_users' => true,
                'remove_users' => true,
                'promote_users' => true,
            )
        );
    }
    add_action('init', 'add_custom_roles');
    

    This code snippet defines a new role called "Manager" and assigns various capabilities to it.

Step 3: Adding and Removing Capabilities

Sometimes, you may need to fine-tune the capabilities of your custom roles. You can add or remove capabilities as needed.

  1. Add Capabilities: To add capabilities to the "Manager" role, use the following code:

    function add_manager_capabilities() {
        $role = get_role('manager');
        $role->add_cap('edit_theme_options');
        $role->add_cap('manage_options');
    }
    add_action('init', 'add_manager_capabilities');
    
  2. Remove Capabilities: To remove capabilities from the "Manager" role, use the following code:

    function remove_manager_capabilities() {
        $role = get_role('manager');
        $role->remove_cap('delete_posts');
        $role->remove_cap('publish_posts');
    }
    add_action('init', 'remove_manager_capabilities');
    

Step 4: Assigning Custom Roles to Users

Once the custom role is created, you can assign it to users through the WordPress admin dashboard or programmatically.

  1. Assign Role via Dashboard:

    • Navigate to Users > All Users.
    • Edit the user you want to assign the role to.
    • In the 'Role' dropdown, select 'Manager' and update the user.
  2. Assign Role Programmatically: You can also assign roles programmatically using the wp_update_user function:

    function assign_manager_role($user_id) {
        $user = new WP_User($user_id);
        $user->set_role('manager');
    }
    

Step 5: Customizing Admin Menus for Custom Roles

To enhance the user experience, you may want to customize the WordPress admin menus for your custom roles. For instance, you can hide certain menu items from the "Manager" role.

  1. Hide Menu Items: Add the following code to hide specific menu items for the "Manager" role:

    function customize_manager_admin_menu() {
        if (current_user_can('manager')) {
            remove_menu_page('tools.php'); // Tools
            remove_menu_page('options-general.php'); // Settings
        }
    }
    add_action('admin_menu', 'customize_manager_admin_menu', 999);
    

Step 6: Creating Custom Capabilities

In some cases, the default capabilities may not be sufficient, and you need to define custom capabilities. Here's how to create and use custom capabilities:

  1. Add Custom Capability: Add the following code to define a new capability:

    function add_custom_capabilities() {
        $role = get_role('manager');
        $role->add_cap('manage_custom_settings');
    }
    add_action('init', 'add_custom_capabilities');
    
  2. Check Custom Capability: Use the current_user_can function to check for the custom capability:

    if (current_user_can('manage_custom_settings')) {
        // User has the capability
    } else {
        // User does not have the capability
    }
    

Step 7: Managing Permissions

Managing permissions effectively ensures that users only have access to the functionality they need. You can control permissions for specific actions using the capabilities you defined.

  1. Restrict Access to a Page: To restrict access to a custom admin page based on capabilities, use the following code:

    function restrict_admin_page_access() {
        if (!current_user_can('manage_custom_settings')) {
            wp_die('You do not have sufficient permissions to access this page.');
        }
    }
    add_action('admin_menu', 'restrict_admin_page_access');
    

Best Practices for Custom User Levels

  1. Define Roles Clearly: Clearly define the purpose and capabilities of each custom role to avoid confusion.
  2. Granular Permissions: Use granular permissions to ensure users only have access to the necessary functionality.
  3. Regular Audits: Regularly audit user roles and capabilities to maintain security and efficiency.
  4. Use Hooks: Utilize WordPress hooks (add_action, add_filter) to manage roles and capabilities dynamically.

Creating custom user levels in WordPress allows you to tailor user permissions to meet the specific needs of your site. By following this guide, you can define new roles, assign capabilities, and manage permissions effectively. This approach not only enhances security but also improves the overall user experience by ensuring users have access to the appropriate functionality. Whether you are managing a small blog or a large multi-user site, custom user roles are a powerful tool in your WordPress arsenal.