Add new role to wordpress
To add a new role to WordPress, you can use the add_role
function, which is part of the WordPress API. Here's an example of how to do it:
function add_new_role() {
$role_name = 'new_role'; // Replace with the name of the new role
$role_description = 'New role description'; // Replace with a description of the new role
$capabilities = array(
'read' => true,
'edit_posts' => true,
'publish_posts' => true,
'delete_posts' => true,
// Add or modify capabilities as needed
);
add_role($role_name, $role_description, $capabilities);
}
add_action('admin_init', 'add_new_role');
This code adds a new role called "new_role" with the specified description and capabilities. The add_role
function takes three arguments:
$role_name
: The name of the new role.$role_description
: A description of the new role.$capabilities
: An array of capabilities that the new role should have. Each capability is a string that corresponds to a capability in WordPress (e.g. "read", "edit_posts", etc.).
You can add or modify capabilities as needed. For example, you might want to add a capability to edit pages, or to manage plugins.
Once you've added the new role, you can assign it to users using the wp_set_role
function:
$user_id = 1; // Replace with the ID of the user you want to assign the role to
wp_set_role($user_id, 'new_role');
This code assigns the "new_role" role to the user with ID 1.
Note that you can also use the add_cap
function to add a capability to an existing role, or to remove a capability from a role. For example:
add_cap('new_role', 'edit_pages');
This code adds the "edit_pages" capability to the "new_role" role.