To add a new custom user role in WordPress, you typically need to use code. Here’s a basic outline of the process:
- 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 );
- 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
);
- 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 );
- Hook into the
initaction: To ensure your custom role is created when WordPress initializes, you should wrap your code in a function and hook it into theinitaction.
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' );
- Test your custom role: After adding the code to your theme’s
functions.phpfile 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.