How to display the WordPress user registration form (the form that appears in “www.mywebsite.com/wp-register.php” page) in the front end of my blog?
I have customised the registration form. But don’t know how to call that form in the front end page. Any support will be really great help.
Thanks in advance. 🙂
The process involves 2 steps:
There are 3 different approach that comes to my mind to show the frontend:
For this answer I’ll use the latter. The reasons are:
1: Build the url
All of us knows that default registration form of a WordPress site is often a target for spammers. Using a custom url is an help to solve this problem. In addition I want also use a variable url, i.e. the registration form url should not be always the same, this makes spammers life harder.
The trick is done using a nonce in the url:
Using this functions is easy to display in templates a link to the registration form even if it’s dynamic.
2: Recognize the url, first stub of
Custom_RegCustom_Reg
classNow we need to recognize the url. For the pourpose I’ll start to write a class, that will be finished later in the answer:
The function look at the first part of the url after
home_url()
, and if it matches with our nonce it return TRUE. this function will be used to check our request and perform needed action to display our form.3: The
Custom_RegForm
classI’ll now write a class, that will be responsible to generate the form markup.
I’ll use it also to store in a property the template file path that should be used to display the form.
The class generate form markup looping all the fields added calling
create
method on each of them.Each field must be instance of
Custom_RegFieldInterface
.An additional hidden field is added for nonce verifiction. Form method is ‘POST’ by default, but it can be setted to ‘GET’ using
setVerb
method.Once created the markup is saved inside the
$form
object property that is echoed byoutput()
method, hooked into'custom_registration_form'
hook: in the form template, simply calldo_action( 'custom_registration_form' )
will output the form.4: The default template
As I said the template for form can be easily overriden, however we need a basic template as a fallback.
I’ll wrote here a very rough template, more a proof of concept than a real template.
5: The
Custom_RegFieldInterface
interfaceEvery field should be an object that implements the following interface
I think that comments explain what classes implementing this interface should do.
6: Adding some fields
Now we need some fields. We can create a file called ‘fields.php’ where we define the fields classes:
I’ve use a base class to define the default interface implemantation, however, one can add very customized fields directly implementing the interface or extending the base class and overriding some methods.
At this point we have everything to display the form, now we need something to validate and save the fields.
7: The
Custom_RegSaver
classThat class, has 2 main methods, one (
validate
) that loop the fields, validate them and saving good data into an array, the second (save
) save all data in database and send password via email to new user.8: Using defined classes: finishing the
Custom_Reg
classNow we can work again on
Custom_Reg
class, adding the methods that “glues” the object defined and make them worksThe constructor of the class accepts an instance of
Form
and one ofSaver
.init()
method (usingcheckUrl()
) look at the first part of the url afterhome_url()
, and if it matches with the right nonce, it checks if the form was already submitted, if so using theSaver
object, it validate and save the userdata, otherwise just print the form.init()
method also fires the action hook'custom_reg_form_init'
passing the form instance as argument: this hook should be used to add fields, to setup the custom template and also to customize the form method.9: Putting things together
Now we need to write the main plugin file, where we can
Custom_Reg
class and callinit()
method on it using a reasonably early hookSo:
10: Missing tasks
Now everithing is pretty done. We have just to customize the template, probably adding a custom template file in our theme.
We can add specific styles and scripts only to the custom registration page in this way
Using that method we can enqueue some js scripts to handle client side validation, e.g. this one. The markup needed to make that script work can be easily handled editing the
Custom_RegBaseField
class.If we want to customize the registration email, we can use standard method and having custom data saved on meta, we can make use of them in the email.
Last task we probably we want to implement is prevent request to default registration form, as easy as:
All the files can be found in a Gist here.
TLDR; Put the following form into your theme, the
name
andid
attributes are important:I found an excellent Tutsplus article on Making a fancy WordPress Register Form from scratch. This spends quite a lot of its time on styling the form, but has the following fairly simple section on the required wordpress code:
Edit: I’ve added the extra final bit from the article to explain where to put the above code snippets – its just a form so it can go in any page template or sidebar or make a shortcode out of it. The important section is the
form
which contains the above snippets and the important required fields.this Article provides a greate tutorial on how to create you own frontend register/login/restore password forms.
or if you are looking for a plugin then i’ve used these before and can recomend them:
I made a website some time ago that was displaying a customized registration form on the front end side. This website is not live anymore but here are some screenshots.
Here are the steps I have followed:
1) Activate the possibility for all visitors to request a new account via Settings > General > Membership option. The registration page now appears at the URL /wp-login.php?action=register
2) Customize the registration form so that it looks like your site front-end. This is more tricky and depends on the theme you are using.
Here is an example with twentythirteen :
Then modify the theme stylesheet to make the form appear as you want.
3) You can further modify the form by tweaking the displayed messages :
4) If you need a front-end registration form, you will probably don’t want that registered users see the backend when they log-in.
There are lots of steps, but the result is here !
Way easier: use a WordPress function called
wp_login_form()
(Codex page here).You can make your own plugin so that you can use a shortcode in on of your pages:
All you have to do is to style your form on the frontend.
If you’re open to the use of plugins, I’ve used the User Registration add-on for Gravity Forms before, it worked very well:
http://www.gravityforms.com/add-ons/user-registration/
Edit: I realise this isn’t a very detailed solution, but it does exactly what you need and is a good solution.
Edit: To expand on my answer further, the User Registration add-on for gravity forms allows you to map any fields in a form created using Gravity Forms, to user-specific fields. For example, you can create a form with First Name, Last Name, Email, Website, Password. Upon submission, the add-on will map those inputs to the relevant user fields.
Another great thing about it, is you can add any registered users into an approval queue. Their user accounts would only be created once approved in the backend by an admin.
If the above link breaks, just Google “User Registration add on for Gravity Forms”