This code hooks into WordPress user creation to safely grant your custom wpfolks role to new users. It checks if they already have the role to prevent unnecessary database work. By using add_role, it adds your role without wiping out existing roles.
add_action( 'user_register', 'wpfolks_assign_custom_role' );
function wpfolks_assign_custom_role( $user_id ) {
$user = new WP_User( $user_id );
// 1. Prevent duplicate runs if the role is already assigned
if ( in_array( 'wpfolks', (array) $user->roles ) ) {
return;
}
// 2. Use add_role() to KEEP the other role AND add your 'wpfolks' role
$user->add_role( 'wpfolks' );
}