Remove or Add user role

A few weeks ago I was creating a business portal for a large company.
The client needed to add all of his employees (300 employees) to the portal and he wanted to assign theme a user role relative to there job function.

So that a sales person will be in Sale, receptionist in receptionist and so on.

I needed to create many new user roles and I needed to get rid of the default wordpress user roles.

This can be done with many plugins out there but I want to do this on my own and not depend on a plugin that can fail after an update.

Here is the code to put inside your function.php to add a new user role.

function wp_codes_add_role() {
add_role( 'receptionist', 'Receptionist',
array(
'read',
'edit_posts',
'delete_posts',
)
);
}
add_action( 'init', 'wp_codes_add_role' );

Inside the function wp_codes_add_role you can add more than one role at the same time.
Here is the reference in the wordpress codex to add role.
Here is the reference in the wordpress codex for all capabilities that you can pass inside the array.

Then I needed to remove all the default roles other then the one I really needed.

function wp_codes_remove_role() {
remove_role( 'editor' );
remove_role( 'author' );
remove_role( 'contributor' );
remove_role( 'subscriber' );
}
add_action( 'init', 'wp_codes_remove_role' );

Here is the reference in the wordpress codex to remove role.


Advertise with us!