For a custom register user form, you can use Advanced Custom Fields to create a front-end form.
You can set up the custom fields for the registration using ACF, and set the location rules for the field group when User Form is equal to Register, and Add/Edit.
I did something similar, hooking into the acf/pre_save_post
filter and doing the following:
- sanitize the inputs
- register the user with
register_new_user()
, which handles the password generation and email notification
- use
wp_update_user()
and update_user_meta()
to map the appropriate custom ACF fields to custom WP user profile fields
- set the new user's role
- use
wp_mail()
to email admin if required
Some more information on registering a user with ACF here.
function my_pre_save_user( $post_id ) {
// If we have an email address, add the user
// This field only exists in the new user field group
if ( isset($_POST['acf']['add acf_field_ID here']) && !empty($_POST['acf']['add acf_field_ID here'])) {
// sanitize our inputs
$sanitized_email = sanitize_email( $_POST['acf']['add acf_field_ID here'] );
$sanitized_firstname = sanitize_text_field( $_POST['acf']['add acf_field_ID here'] );
$sanitized_lastname = sanitize_text_field( $_POST['acf']['add acf_field_ID here'] );
$sanitized_contactnumber = sanitize_text_field( $_POST['acf']['add acf_field_ID here'] );
// Prepare basic user info
$username = $sanitized_email;
$email = $sanitized_email;
$display_name = $sanitized_firstname .' '. $sanitized_lastname;
// Register the user and store the ID as $user_id, handles the validation, generates random password and send email notification to user
$user_id = register_new_user( $username, $email );
// If we get an error (eg user already exists)
if( is_wp_error( $user_id ) ) {
// Show the error
echo 'Error: '.$user_id->get_error_message();
// Exit
exit;
// Good to go
} else {
// get single value from post object
$dmc_get_company_field = $_POST['acf']['add acf_field_ID here'];
$dmc_selected_exhibitor = get_field( $dmc_get_company_field );
// Update the new user's info
wp_update_user( array(
'ID' => $user_id,
'first_name' => $sanitized_firstname,
'last_name' => $sanitized_lastname,
'display_name' => $display_name
));
// update the new users's meta
update_user_meta( $user_id, 'dmc_exhibitor_company_name', $dmc_get_company_field );
update_user_meta( $user_id, 'dmc_exhibitor_contact_number', $sanitized_contactnumber );
// update user role
$user_id_role = new WP_User( $user_id );
$user_id_role->set_role( 'contributor' );
$profile_link = get_edit_user_link( $user_id );
$to = "[add email addresses here]";
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$headers[] = 'Reply-To: '. $username. ' <'. $email .'>';
$subject = "[add email subject here]";
$body = "[add email body here]";
// send the email
wp_mail( $to, $subject, $body, $headers );
// redirect to thankyou page
$redirect_url = get_bloginfo('url') . '[add URL to redirect to here]';
wp_redirect( $redirect_url );
// exit this script
exit;
}
} else {
return $post_id;
}
}
add_filter('acf/pre_save_post' , 'my_pre_save_user' );