To add new custom role in wordpress

To add a new custom user role in WordPress, you typically need to use code. Here’s a basic outline of the process:

  1. Create a new custom role: You can use the add_role() function in WordPress to create a new role. This function accepts four parameters: the role name, the display name, capabilities (optional), and the capabilities that role should inherit (optional).
php
Copy code
add_role( $role, $display_name, $capabilities );
  1. Assign capabilities: If you want your custom role to have specific capabilities, you need to define them. WordPress provides a wide range of capabilities that you can assign to roles. You can define capabilities yourself or use existing ones.
php
Copy code
$capabilities = array(
    'read' => true,
    'edit_posts' => true,
    // Add more capabilities as needed
);
  1. Add capabilities to the custom role: After defining capabilities, you can pass them to the add_role() function.
php
Copy code
add_role( 'custom_role', 'Custom Role', $capabilities );
  1. Hook into the init action: To ensure your custom role is created when WordPress initializes, you should wrap your code in a function and hook it into the init action.
php
Copy code
function create_custom_role() {
    $capabilities = array(
        'read' => true,
        'edit_posts' => true,
        // Add more capabilities as needed
    );
    add_role( 'custom_role', 'Custom Role', $capabilities );
}
add_action( 'init', 'create_custom_role' );
  1. Test your custom role: After adding the code to your theme’s functions.php file or a custom plugin, you should test to ensure the new role functions as expected. You can create a new user and assign them the custom role to verify that they have the capabilities you’ve defined.

Leave a comment

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