Advanced Forms Pro provides a UI for creating a single user on submission but if you need to create more than one user, you'll need to write a custom submit handler as demonstrated below. Consider the following field group set up with a repeater field to accept user data:
Note that our form has Enable user editing? disabled as we will be handling the user creation via PHP:
The user creation code is quite simple. It gets the data from the repeater field, loops through each row and uses the sub fields to insert a new user. It also triggers WordPress' core new user email notifications:
add_action( 'af/form/submission', function ( $form, $fields, $args ) { // If we need to run this only on a specific form, check for the form key. if ( $form['key'] !== 'YOUR_FORM_KEY_HERE' ) { return; } // Get new user data from a repeater field. $new_users = af_get_field( 'new_users' ); if ( empty( $new_users ) ) { return; } // For each repeater row, insert a new user. foreach ( $new_users as $new_user ) { // Insert the user, getting the new user ID. $user_id = wp_insert_user( [ 'role' => 'subscriber', 'user_pass' => wp_generate_password(), 'first_name' => $new_user['first_name'], 'last_name' => $new_user['last_name'], 'user_email' => $new_user['email'], 'user_login' => $new_user['email'], // Note: this is mandatory or the user will not be created. ] ); if ( is_wp_error( $user_id ) ) { // Handle error here, if desired. continue; } // Send the new user emails to both the user and to site admin. wp_send_new_user_notifications( $user_id, 'both' ); } }, 10, 3 );
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article