Do you want to create a custom front-end user registration form in WordPress? The default WordPress user registration form shows WordPress branding and does not match your site’s theme. In this article, we will show you how to create a custom user registration form in WordPress.
Why Add a Custom User Registration Form in WordPress
The default WordPress user registration page shows WordPress branding and logo. It does not match the rest of your WordPress site.
Creating a custom user registration form allows you to add the registration form on any page of your WordPress site. It helps you deliver a more consistent user experience during the registration process.
A custom user registration form also allows you to redirect users upon registration.
Having said that, let’s take a look at how to create a custom user registration form in WordPress.
Having said that, let’s take a look at how to create a custom user registration form in WordPress.
Also you need to use this code plugin after that you can follow this video. here is my code
<?php
/**
* Plugin Name: Elementor Form Create New User
* Description: Create a new user using elementor form
* Author: Happy Arif
* Author URI: https://happyarif.com
* 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
}