Get 20% Off On All Our Premium Themes For Limited Time Only, Use Coupon Code: COVID19

How to Create Custom User Registration Form in WordPress?

How to Create a Custom User Registration Form in WordPress

WordPress provides an easy way to create custom user registration forms for your website. This can be useful if you want to allow users to sign up for your site, access restricted content, or provide feedback. Creating a custom user registration form in WordPress can be done with a few different methods, including using a plugin or custom code.

Using a Plugin

One of the easiest ways to create a custom user registration form in WordPress is by using a plugin. There are many free and premium plugins available that can help you create custom forms quickly and easily. Here are a few popular options:

Once you have installed and activated the plugin, you can create a new form and add the fields you want to include in the registration process, such as name, username, email address, and password. The plugin will handle the form validation and user creation, so you don’t have to write any custom code.

Custom Code

If you’re comfortable with coding, you can also create a custom user registration form in WordPress using custom code. This method requires a bit more work, but it gives you complete control over the form and the registration process. Here’s how you can create a custom user registration form:

  1. Create a new page or post for the form
  2. Add the following code to the page or post:
<form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="post">
  <input type="hidden" name="action" value="register_user">
  <p>
    <label for="username">Username</label>
    <input type="text" id="username" name="username">
  </p>
  <p>
    <label for="email">Email</label>
    <input type="email" id="email" name="email">
  </p>
  <p>
    <label for="password">Password</label>
    <input type="password" id="password" name="password">
  </p>
  <p>
    <input type="submit" value="Register">
  </p>
</form>
  1. Add the following code to your theme’s functions.php file to handle the form submission:
add_action( 'admin_post_register_user', 'register_user_function' );
add_action( 'admin_post_nopriv_register_user', 'register_user_function' );
function register_user_function() {
  // Validate form fields
  if ( empty( $_POST['username'] ) || empty( $_POST['email'] ) || empty( $_POST['password'] ) ) {
    // Error: missing fields
    wp_redirect( add_query_arg( 'register_error', 'missing_fields', wp_get_referer() ) );
    exit;
  }

  // Check if the email address is already in use
  if ( email_exists( $_POST['email'] ) ) {
    // Error: email already in use
    wp_redirect( add_query_arg( 'register_error', 'email_exists', wp_get_referer() ) );
    exit;
  }

  // Create the user
  $user_id = wp_insert_user( array(
    'user_login' => $_POST['username'],
    'user_email' => $_POST['email'],
    'user_pass'  => $_POST['password'],
  ) );

  if ( is_wp_error( $user_id ) ) {
    // Error: unable to create user
    wp_redirect( add_query_arg( 'register_error', 'create_user_failed', wp_get_referer() ) );
    exit;
  }

  // Redirect to success page
  wp_redirect( add_query_arg( 'register_success', 'true', wp_get_referer() ) );
  exit;
}

With this code, you can handle form submissions and validate the form fields, including checking if the email address is already in use. If the form submission is successful, a new user will be created and the user will be redirected to a success page. You can also add error handling to display error messages if the form submission fails.

Conclusion

Creating a custom user registration form in WordPress is a simple process that can be done with a plugin or custom code. Using a plugin can save you time and effort, while custom code gives you complete control over the form and the registration process. Whichever method you choose, having a custom user registration form on your site can provide a better user experience for your visitors and help you gather information from your users.