Similarly to adding fields to the user editor, WordPress offers two different action hooks to register functions that will be called when a user is initially registered (user_register) and when a user profile is updated (profile_update). As you can see, we are registering the same callback function in both cases, where we check to see whether we received a value indicating the desired user level and use the update_user_meta function to save it. Similar to the post and term meta update functions, this function has three parameters to indicate the user ID that the information should be associated with, the name of the custom data field, and the value to be stored:
update_user_meta( $user_id, $field_name, $single );
As an additional precaution, we check to see whether the value we received is present in our global array of user levels using the PHP array_key_exists function.
The else branch of the if statement is put in place to set a default user level if the field is absent, which can happen if the site allows visitors to register for accounts by themselves.
Of course, this current process of classifying users as regular users or paid users is very manual, requiring site administrators to log in to the site's dashboard and update their status individually. A more elaborate solution could be to set the value of this field based on receiving a payment to the site using an online payment service.