Problems using ‘add_role’

You can pass the user’s role directly to wp_insert_user(). First, you can try to add your custom role to the WP_Roles object, then pass the custom role to the user properties:

// Add custom roles only once to avoid unnecessary database calls.
// It is usually done during plugin activation.
register_activation_hook( __FILE__,  'cyb_add_roles' );
function cyb_create_role() {

    // array with capabilities
    $lojas_caps = array();

    add_role( 'lojas', 'Lojas', $lojas_caps );

}

function ds_registration_form(&$fields_user, &$errors) {

    // ....

    $fields_loja['role'] = 'lojas';
    $loja_id = wp_insert_user( $fields_loja );

    // Or it can be set to default role and then
    // add the custom role as additional role
    // $loja_id = wp_insert_user( $fields_loja );
    // if( ! is_wp_error( $loja_id ) ) {
    //     $user = new WP_User( $loja_id );
    //     $user->add_role( 'lojas' );
    // }


    // ....
}

tech