Skip to content

Instantly share code, notes, and snippets.

@adriannees
Last active September 11, 2023 11:04
Show Gist options
  • Select an option

  • Save adriannees/e0ddcffdf270e7202aa19c46fe6594c5 to your computer and use it in GitHub Desktop.

Select an option

Save adriannees/e0ddcffdf270e7202aa19c46fe6594c5 to your computer and use it in GitHub Desktop.
Elementor Pro Form - New User Registration
// Add this to a code snippet via the Code Snippets plugin (modified from (from https://alfatahnesab.com/how-to-create-elementor-user-registration-form/)
// This is the hooks of elementor form after form submit (from https://alfatahnesab.com/how-to-create-elementor-user-registration-form/)
add_action( 'elementor_pro/forms/new_record', 'alfa_elementor_form_create_new_user' , 10, 2 );
function alfa_elementor_form_create_new_user($record,$ajax_handler) // creating function
{
$form_name = $record->get_form_settings('form_name');
//Check that the form is the "Create New Subscriber" or "Create New Editor" (or whichever form names you want) if not - stop and return;
if ('Create New Subscriber' !== $form_name && 'Create New Editor' !== $form_name) { // for a single form remove the && and second form
return;
}
$form_data = $record->get_formatted_data();
//Get the value of the inputs with the label. Add or remove variables as appropriate for your use.
$username=$form_data['Username']; // Label "Username"
$password = $form_data['Password'];
$email=$form_data['Email'];
$first_name=$form_data["First Name"];
$last_name=$form_data["Last Name"];
$role=$form_data["Role"];
$company=$form_data["Brand"];
$company_url=$form_data["Website"];
$instagram=$form_data["Instagram"];
$facebook=$form_data["Facebook"];
$twitter=$form_data["Twitter"];
$pinterest=$form_data["Pinterest"];
$address_one=$form_data["Address One"];
$address_two=$form_data["Address Two"];
$city=$form_data["City"];
$state=$form_data["State"];
$country=$form_data["Country"];
$zip=$form_data["Zip Code"];
$phone=$form_data["Phone"];
// Create the new user in WordPress
$userdata = array(
'user_login' => wp_slash($username),
'user_pass' => $password,
'user_email' => wp_slash($email),
'first_name' => wp_slash($first_name),
'last_name' => wp_slash($last_name),
'role' => $role,
);
$user_id = wp_insert_user($userdata);
// Create a new user, on success return the user_id or on failure return an error object
if (is_wp_error($user_id)){ // if there was an error creating a new user
$ajax_handler->add_error_message("Failed to create new user: ".$user_id->get_error_message()); //add the message
$ajax_handler->is_success = false;
return;
}
// Add user related information which is called user meta data with the function update_user_meta
// To add user meta data we need to get the value of meta values in the variable. Add any relevant usermeta. Like below:
$metas = array (
"billing_company" => $company,
"billing_email" => $email,
"billing_first_name" => $first_name,
"billing_last_name" => $last_name,
"billing_address_1" => $address_one,
"billing_address_2" => $address_two,
"billing_city" => $city,
"billing_state" => $state,
"billing_country" => $country,
"billing_postcode" => $zip,
"billing_phone" => $phone
);
foreach($metas as $key => $value) {
update_user_meta( $user_id, $key, $value );
}
// Automatically log in the user and redirect the user to the home page
$creds= array( // credientials for new user
"user_login" => $username,
"user_password" => $password,
"remember" => true
);
// Set the redirect URL with the form fields shortcodes
$redirect_to = $record->get_form_settings( 'redirect_to' );
$signon = wp_signon ($creds, is_ssl() ); // sign in the new user
if ($signon)
// Set redirect action
$ajax_handler->add_response_data( 'redirect_url', $redirect_to );
}
@adriannees
Copy link
Author

updating wp_create_user to wp_insert_user, thereby eliminating the wp_update_user call

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment