<?php
/**
* Plugin Name: Elementor Form Create New User
* Description: Create a new user using elementor form
* Author: Gal Hadad
* Author URI: https://wpsite.co.il
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
add_action( 'elementor_pro/forms/new_record', 'thewpchannel_elementor_form_create_new_user' , 10, 2 );
function thewpchannel_elementor_form_create_new_user($record,$ajax_handler)
{
$form_name = $record->get_form_settings('form_name');
//Check that the form is the "create new user form" if not - stop and return;
if ('Create New User' !== $form_name) {
return;
}
$form_data = $record->get_formatted_data();
$username=$form_data['User Name']; //Get tne value of the input with the label "User Name"
$password = $form_data['Password']; //Get tne value of the input with the label "Password"
$email=$form_data['Email']; //Get tne value of the input with the label "Email"
$user = wp_create_user($username,$password,$email); // Create a new user, on success return the user_id no failure return an error object
if (is_wp_error($user)){ // if there was an error creating a new user
$ajax_handler->add_error_message("Failed to create new user: ".$user->get_error_message()); //add the message
$ajax_handler->is_success = false;
return;
}
$first_name=$form_data["First Name"]; //Get tne value of the input with the label "First Name"
$last_name=$form_data["Last Name"]; //Get tne value of the input with the label "Last Name"
wp_update_user(array("ID"=>$user,"first_name"=>$first_name,"last_name"=>$last_name)); // Update the user with the first name and last name
/* Automatically log in the user and redirect the user to the home page */
$creds= array( // credientials for newley created user
"user_login"=>$username,
"user_password"=>$password,
"remember"=>true
);
$signon = wp_signon($creds); //sign in the new user
if ($signon)
$ajax_handler->add_response_data( 'redirect_url', get_home_url() ); // optinal - if sign on succsfully - redierct the user to the home page
}
Create Registration Form Using Elementor Pro
- By Gal Hadad
- March 3, 2019
- 9:31 pm
- 17 Comments
Video tutorial about how to use PHP and Elementor Pro forms, in order to create a registration form.
Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on print
Print
Gal Hadad
My name is Gal and I am a WordPress developer.
I’ve been working with WordPress since 2008.
I’m a programmer who loves UX. I have a BS.c in Information Systems and I’m currently working on my master’s at HCI.
All Posts »
17 thoughts on “Create Registration Form Using Elementor Pro”
Hi Gal,
Nice tutorial! Many thanks for sharing your knowledge.
I found your article by searching for a solution related to this webhook and a wp_query.
I am creating a giveaway in my WordPress installation. When a user entry is created I save it to a CPT (my code is similar to yours, uglier, but similar), that works great.
But, I have another post type that is the giveaway, where I set the prize, the winners quantity, etc. I have another Elementor form there which picks the winner with a wp_query over the user entries CTP. I use the same webhook for both forms, filtering by the form name to know when to create an entry or when to pick winner. The thing is that when trying to get post by wp_query to pick a winner I get a nasty 500 server error “Failed to load resource: the server responded with a status of 500 (/wp-admin/admin-ajax.php:1 )”.
So, I have 2 questions:
1. The easy one, how can I make this work? I would be nice to teak a few things and don’t trough my work away.
2. The hard one, Is this the correct way to achieve this? Should I start over with a different approach?
Many thanks!.
Best
Hi Gustavo, Server Error 500, is just the server way to say “there was an error”, You should check the error logs in the server to find out more about it.
Or you can put your site in debug mode by pasting this code in wp-config.php (only do this if your site isn’t live):
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', true);
@ini_set('display_errors', E_ALL);
Overall your method should work.
Thank you Gal!
Hi Gal! Nice work
Is there a way to create a used database separate from the main user database? I want the people who sign up as users to be different from the page admins and once they sign up to be taken to a separate dashboard where they can see some info specifically to them. Is there a way to do that or should I think about designing a whole interface from scratch with PHP?
Hi Cosim, first of all, yes you can use a separate database, but it’s not the way you should do it.
You should create a role, for example “special_user” and give that role the permissions that you want.
Then when user registrated using the elementor form, you make sure they are the new you role that you created.
See this for more information: https://wordpress.org/support/article/roles-and-capabilities/
Hey Gal, i watched your video and it is very nice, thank you for the help but i have a problem, i created a popup form with elementor pro and do all steps like you in the video but it nothing happens. I activated the plugin and checked the form name but it will not create any new user… Can u help me pls? 🙂
Hi Pascal, it should work with a popup. Can you post your code here?
Hello Gal, thx for share this tutorial, im very helped about this, i want asking
– how if im buy for custome field like combo box,button box, Select or anytype ?
example > http://prntscr.com/oze0h6
Thank u so much…
You save me with this tutorial, its complete and comprensible (sorry for my bad english)
I have a question, its posible add other camp for password (repeat password) or it will afect the fuction of the code/registration
greatings
oh and is there a form to change the destination of the redirection after registration (not redirection to home, insted my shop for example)
Hi Jorge, I’m just another guest here that is helped by this very post by Haddad.
For password verification you can add another field (duplicate the password field) and then do validation in your function, or you can do it with javascript/jQuery to check if those two fields have same value.
for redirection you can add this:
$ajax_handler->add_response_data( ‘redirect_url’, home_url( ‘/my-shop’ ) );
Hey Gal. great code!
Can you give me a hint on how to register some of woocommerce fields?
for example: name=”billing_phone” id=”billing_phone”
Thanks!
Sorry, and one more thing, how do I update user role?
I have looked at the wp_update_user page, but could not find an example.
Thanks
I believe those woocommerce fields are user meta (cmiiw), use this:
update_user_meta( $user, ‘meta_key’, ‘meta_value’ );
//$user is user id, returned by wp_create_user()
//replace meta_key with ‘billing_phone’ or any other user meta key
//replace meta_value with variable containing billing_phone, e.g. $form_data[‘billing_phone’]
Use this to update user role:
wp_update_user( array(
‘ID’ => $user, //user id returned by wp_create_user()
‘role’ => ‘new_user_role’, //user role slug
)
);
Yet to test this Gal, but thankyou so much for creating this custom code/plugin! It really is sad that Elementor Pro doesn’t have a registration field in their plugin. I’ll let you know how I go!
Thank you for sharing!
hi gal, I tested the plugin and it works fine, but it needs some fixe
the code do creat the user in the WordPress with subscriber role,
although the form doesn’t allow to add existed user to the database
and display error message “Failed to create a new user: Sorry, that username already exists!”
I found that if I set after submit action “email” to send the registred confirmation email
the email still sent
how to make a condition to the after submitting action “email”, to be sent only if the user-created