Customize WordPress Registration Form

Although WordPress provides default user registration mechanism, sometimes we need to add custom fields to the registration form to collect more information from the user. Here we give a design pattern to create a wordpress custom signup form. We use the same registration page to display the front end and handle the user input information at the back end. All the registration form code will be included in a class MyRegistration.

class MyRegistration
  function  _construct($template){};
  function  register()
    if(form fields are empty) return;
    create a new user;
    redirect to home page;

In the constructor, we will add a hook function register() to the action template_redirect, which is called before the our registration template is loaded. In the hooked function, we will check if the user has filled the fields in the signup form, if so, will create a new user using wordpress internal function wp_create_user,  then  redirect to the home page by calling wp_redirect.  To let the user keep in login state, we should call wp_set_auth_cookie to set the cookies. If the user has not filled the fields yet, just return to load the template to display the registration form.

The custom registration form front end is provided as a page template, so you should create a register page in the administration panel and associate the page with the template. Then create a MyRegistration object passing the template name as the parameter to the constructor. The registration template is a html form with the action set to the url of the registration page and with a hidden input set to the redirection url so that we can retrieve the url at the back end and do the redirection in the register() function.

If you like my content, please consider buying me a coffee. Buy me a coffeeBuy me a coffee Thank you for your support!

Leave a Reply