WordPress is renowned for its flexibility and ease of use, making it one of the most popular content management systems in the world. However, there are times when the default features may not fully meet the unique needs of a website. One such scenario is the registration process. While WordPress provides a built-in registration form, it offers limited customization options, which may not be sufficient for more complex requirements.
Creating a custom registration form through your own plugin allows you to tailor the experience to your exact needs. Whether it’s adding additional fields like a phone number or full name, or integrating specific validation rules, a custom form gives you complete control.
In this article, we’ll guide you through the process of building a custom register form in WordPress using a plugin, offering you both flexibility and scalability for your website.
Read More: MySQL Stored Procedure Complete Video Courses (Free)
Let’s get started.
Why Create a Custom Register Form?
The default WordPress registration form works fine for basic setups, but there are many scenarios where a custom form is more suitable:
- Custom Fields: You may need fields like phone number, full name, or other project-specific data.
- Enhanced User Experience: A tailored form can better match your website’s design and branding.
- Control Over Validation and Storage: Custom forms let you define how data is validated and stored, ensuring compatibility with your specific needs.
Prerequisites
Before diving into the process, ensure you have:
- Basic knowledge of PHP and WordPress.
- Familiarity with WordPress hooks and actions.
- Access to a WordPress installation for testing.
Steps to Create a Custom Register Form
Setting Up the Plugin
- Create a new folder in the
wp-content/plugins
directory. Name itcustom-register-form
. - Inside this folder, create a PHP file named
custom-register-form.php
. - Add the following code to initialize the plugin:
<?php
/**
* Plugin Name: Custom Register Form
* Description: A plugin to create a custom registration form in WordPress.
* Version: 1.0
* Author: Your Name
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
Read More: CodeIgniter 4 REST APIs Development Video Course (Free)
Creating the Registration Form
Add a function to render the custom registration form using a shortcode:
function custom_register_form() {
ob_start();
?>
<form method="post" action="">
<label for="full_name">Full Name:</label>
<input type="text" name="full_name" id="full_name" required />
<label for="phone_number">Phone Number:</label>
<input type="text" name="phone_number" id="phone_number" required />
<label for="email">Email:</label>
<input type="email" name="email" id="email" required />
<label for="password">Password:</label>
<input type="password" name="password" id="password" required />
<button type="submit" name="custom_register_submit">Register</button>
</form>
<?php
return ob_get_clean();
}
add_shortcode('custom_register_form', 'custom_register_form');
Handling Form Submission
Capture and validate form data:
function handle_custom_register_form() {
if (isset($_POST['custom_register_submit'])) {
$full_name = sanitize_text_field($_POST['full_name']);
$phone_number = sanitize_text_field($_POST['phone_number']);
$email = sanitize_email($_POST['email']);
$password = sanitize_text_field($_POST['password']);
if (!is_email($email)) {
echo "Invalid email address.";
return;
}
$user_id = wp_create_user($email, $password, $email);
if (!is_wp_error($user_id)) {
update_user_meta($user_id, 'full_name', $full_name);
update_user_meta($user_id, 'phone_number', $phone_number);
echo "Registration successful.";
} else {
echo "Error: " . $user_id->get_error_message();
}
}
}
add_action('init', 'handle_custom_register_form');
Adding Custom Validation
Ensure the data is valid before saving:
function validate_custom_fields($errors, $sanitized_user_login, $user_email) {
if (empty($_POST['full_name'])) {
$errors->add('full_name_error', '<strong>ERROR</strong>: Full Name is required.');
}
if (empty($_POST['phone_number'])) {
$errors->add('phone_number_error', '<strong>ERROR</strong>: Phone Number is required.');
}
return $errors;
}
add_filter('registration_errors', 'validate_custom_fields', 10, 3);
Saving User Data
Store custom field data using update_user_meta
:
function save_custom_user_meta($user_id) {
if (isset($_POST['full_name'])) {
update_user_meta($user_id, 'full_name', sanitize_text_field($_POST['full_name']));
}
if (isset($_POST['phone_number'])) {
update_user_meta($user_id, 'phone_number', sanitize_text_field($_POST['phone_number']));
}
}
add_action('user_register', 'save_custom_user_meta');
Conclusion
Creating a custom registration form in WordPress using your own plugin is a straightforward process that offers incredible flexibility. With the steps above, you can build a tailored registration process that aligns perfectly with your project’s requirements.
That’s it.
We hope this article helped you to learn Create a Custom Register Form in WordPress (Super Easy) in a very detailed way.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.