Creating a Custom WordPress Registration Form Plugin

Out-of-the-box, WordPress provides a custom registration form that can be used to setup a new user, or to add a new user when adding them to an existing WordPress installation. But what if you want to implement a custom registration form that does not render the options in the WordPress Dashboard?

In this tutorial, we'll learn how to create a custom registration form in WordPress using a combination of template tags and shortcodes.

The default registration form consist of just two form fields - username and email.

The presence of only the username and email form field make the registration process incredibly easy. First, you enter your username and email after which a password will be mailed to you. Next, you login to the site with the password and then complete your profile and change the password to something memorable.

Instead of making a user to go through the above stress just to register in your site, why not provide a straight-to-the-point registration form with consisting of some important extra form fields in addition to the default username and email such as a password, a URL to their website, a biography, a nickname, and their first and last name.

This is particularly useful in a multi-author website like Tuts+.

In this article, we will be building a custom registration form plugin with the following form fields: 

  • username
  • password
  • email
  • website URL
  • first name
  • last name
  • nickname
  • biography (or an about section)

The custom registration form can then be integrated into WordPress using the plugin shortcode and associated template tag.

With the shortcode, you can create a page and make it the official registration page of your site. You can also use the shortcode within a post so user can signup to your site after reading one of your articles.

If you want to add the registration form to the sidebar or a specific location in your website, then you can edit the WordPress theme but placing the template tag in the desired location. 

Before we start building the registration form plugin, it is worth noting that the username, password, and email field are required.

We will be enforcing this rule when writing our validation function.

Premium Option

This tutorial will teach you to build the plugin from scratch, but if you're looking for a quick, plug-and-play solution, try the WordPress Registration Form plugin on Envato Market. You can set a wide range of registration fields with validation control. Once registration is completed, an email is sent to the new member with their login details. Email templates can be modified for registration, password change, etc.

WordPress Registration Form plugin on Envato Market
WordPress Registration Form plugin on Envato Market

Another option is simply to place an order on Envato Studio. You can choose the right person from a wide range of experienced WordPress plugin developers. Then you send your brief, and let the developer create your plugin within the agreed timeframe.

For example, Alisaleem252 will develop a customized WordPress plugin which will be compatible with latest WordPress version and other plugins in the WordPress Repository according to your requirements. 

You'll get: 

  • custom widget if it is needed 
  • custom shortcode if it is needed 
  • custom post type if it is needed 
  • reliable service 
Custom WordPress Plugin Development
Custom WordPress Plugin Development

The full service costs just $300, and your plugin will be ready in 10 days. Alisaleem252 has a 98% approval rating from previous clients. So why not try out his popular Custom WordPress Plugin Development service!

Building the Plugin

With that said, let's get started with coding of the plugin. First, include the plugin header.

Next, we create a PHP function that contains the HTML code of the registration form.

Notice the registration field being passed to the above function as variable? In the function code, you will see instances of the following code, for example:

( isset( $_POST['lname'] ) ? $last_name : null ) 

The ternary operator is checking the contents of the global $_POST array to see if the form contains a value. If it does contain a value, it populates the form fields with the value in order to save the user from re-entering the field input.

A registration form is never complete until you validate and sanitize the user input. As a result, we will create a validation function with name registration_validation.

To ease the validation pain, we will be using WordPress WP_Error class. Follow me as we code the validation function:

  1. Create the function and pass the registration field as the function argument.
  2. Instantiate the WP_Error class and make the instance variable global so it can be access outside the scope of the function.
  3. Remember: We said the username, password and email are required and must not be left out. To enforce the rule, we need to check if any of the field is empty. If empty, we append the error message to the global WP_Error class.
  4.  We also check to make sure the number of username characters is not less than 4.
  5. Check if the username is already registered.
  6. Employ the services of WordPress validate_username function to make sure the username is valid.
  7. Ensure the password entered by users is not less than 5 characters.
  8. Check if the email is a valid email.
  9. Check if the email is already registered.
  10. If the website field is filled, check to see if it is valid.
  11. Finally, we loop through the errors in our WP_Error instance and display the individual error.
    We are done coding the validation function.

Next is the plugin complete_registration() function that handles the user registration.
The user registration is actually done by the wp_insert_user function which accepts an array of user data.

In the complete_registration() function above, we made the $reg_errors WP_Error instance and the form fields variable global so we can access the variable in the global scope.

We then check if the $reg_errors error handling instance contains any error. If no error is found, we proceed to populating the $userdata array and insert the user registration details to WordPress database and display a Registration Complete message with a link to the login page.

Up next, is the super custom_registration_function() function that puts all the functions we've created above into use.

Let me explain what the code in the custom_registration_function() function.

First, we determine if the form has been submitted by checking if the $_POST['submit'] is set. If the form has been submitted, we call the registration_validation function to validate the user submitted form.

We then sanitize the form data and set the sanitize data to a variable named after the form field. Finally, we call the complete_registration to register the user.

We need to call the registration_form function to display the registration form.

Remember I mentioned that we are going to be providing shortcode support for the registration plugin? Below is the shortcode support code.

At this point, we are done coding the plugin. Below is an image showing how the registration form look like.

Note that you might not get the exact same look on your WordPress site as a result of vary CSS styling.

Plugin Usage

To implement the plugin in a WordPress post or page, use the shortcode [cr_custom_registration].

To implement the registration form in a specific aspect of your theme, add the following template tag - <?php custom_registration_function(); ?>.
You can get the plugin file from the attachment in this article.

Summary

In this article, we walked through the process of creating a plugin that adds a custom registration form to WordPress. You can further extend the registration form to include extra fields such as user role, AOL IM account, but make sure the form field is a valid meta data for wp_insert_user.

If you have any questions and suggestions, let me know in the comments!

Remember that if you struggled to follow this tutorial and you want a simple solution, try the WordPress Registration Form plugin on Envato Market. 

Tags:

Comments

Related Articles