Planning the registration process
In this application, we have opted to build a complex registration process in order to understand the typical requirements of web applications. So, it's better to plan it upfront before moving into the implementation. Let's build a list of requirements for registration:
- The user should be able to register as any of the given user roles
- If the user registers as a free member, the activation code needs to be generated and sent to the user
- If the user registers as a premium member, the user will be redirected to a separate page for payments, and notification will not be sent until the user completes the payment
- The default notification on successful registration needs to be customized to include the activation link
- Users should activate their account by clicking the link
So, let's begin the task of registering users by displaying the registration form inside the WPWAF_Registration class as given in the following code:
public function display_registration_form(){
global $wpwaf_registration_params;
if ( !is_user_logged_in() ) {
include WPWAF_PLUGIN_DIR . 'templates/register-template.php';
exit;
}
}
Once the user requests /user/register, the controller calls the display_registration_form function after the do_action call. In the initial request, we need to check whether a user is already logged in using the is_user_logged_in function. If not, we can directly include the registration template located inside the templates folder to display the registration form.
WordPress templates can be included using the get_template_part function. However, it doesn't work like a typical template library, as we cannot pass data to the template. In this technique, we are including the template directly inside the function. Therefore, we have to use a global variable to pass and access the data between the class and the template.