How to create a new user role in WordPress without using any plugin?

To define a new user role in WordPress, just copy and paste this code snippet in the functions.php file of the active theme. In this example we will create B2B user role with only read capabilites.

//Custom roles
function add_new_user_roles_wp() {
    add_role( 'b2b', __('B2B'), array('read' => true,));
add_action( 'init', 'add_new_user_roles_wp' );

How to create a new user role with exactly same capabilities of an exsiting user role?

You may encounter this scenario while developing a custom website or an ecommerce store using woocommerce where you need to create a separate user role with same capabilities of any existing role. In this example we we will again create B2B user role but this time with the woocommerce customer user role’s capabilities.

//New user role with same capabilties of woocommerce customer role
function add_new_user_role_wp() {
    add_role( 'b2b', 'B2B', get_role( 'customer' )->capabilities );
add_action( 'init', 'add_new_user_role_wp' );
Need a helping hand in fixing your website issues?

If you are facing any problems in implementing these code snippets and tutorials, you can hire us to fix your website issues.

Hire Us