Automatic change of user level

Hello,
I need to change the user level right after registration to another level.
As I searched for this a while I thought i push this solution into the forum to make it searchable for others.

Here is the function to be placed in functions.php.

// Update user cap to level 1 after registration
add_action( 'user_register', 'yourtheme_registration_save', 10, 1 );
 
function yourtheme_registration_save( $user_id ) {
    $user = new WP_User($user_id);
    $user->set_role("s2member_level1");
}