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 Professional Support for Your Website Problems?
Whether you're facing website issues or struggling with code implementation, our team is here to assist. Hire us to get your website back on track.
Hire Us