Wordpress Web Application Development(Third Edition)
上QQ阅读APP看书,第一时间看更新

Exploring the registration success path

Now let's look at the success path, where we don't have any errors, by looking at the remaining sections of the register_user function:

    if ( empty( $errors ) ) { 
$user_pass = wp_generate_password();
$user_id = wp_insert_user( array('user_login' =>
$sanitized_user_login,
'user_email' => $user_email,
'role' => $user_type,
'user_pass' => $user_pass)
);
if ( !$user_id ) {
array_push( $errors, __('Registration failed.','wpwaf') );
$wpwaf_registration_params['errors'] = $errors;
} else {
$activation_code = $this->random_string();
update_user_meta( $user_id, 'wpwaf_activation_code',
$activation_code );
update_user_meta( $user_id, 'wpwaf_activation_status', 'inactive'
);

if($user_type == 'wpwaf_premium_member'){
update_user_meta( $user_id, 'wpwaf_payment_status', 'inactive' );
// Redirect User to Payment page with User Details
}else{
update_user_meta($user_id,'wpwaf_payment_status','active' );
wpwaf_send_user_notification ( $user_id, $user_pass, $activation_code );
$wpwaf_login_params['success_message'] = __('Registration completed successfully. Please check your email for activation link.','wpwaf');

}
}
if ( !is_user_logged_in() ) {
include WPWAF_PLUGIN_DIR . 'templates/login-template.php';
exit;
}
}

We can generate the default password using the wp_generate_password function. Then, we can use the wp_insert_user function with respective parameters generated from the form to save the user in the database.

The wp_insert_user function will be used to update the current user or add new users to the application. Make sure you are not logged in while executing this function; otherwise, your admin will suddenly change into another user type after using this function.

If the system fails to save the user, we can create a registration fail message and assign it to the $errors variable as we did earlier. Once the registration is successful, we will have to work with two separate paths for our member roles.

If the user is registered as a premium member, we set the initial wpwaf_payment_status as inactive and redirect the user to the payments screen. The payments screen and submission are not handled at this stage in the book.

If the user is registered as a free member, we set the initial wpwaf_payment_status as active and generate a random string as the activation code. You can use any function here to generate a random string.

Then, we update the user with an activation code and set the activation status as inactive for the moment. Finally, we have to send an e-mail containing the registration details. By default, this e-mail is sent by the wp_new_user_notification function inside the WordPress core. Here, we are going to use our own email function to include the custom content and make it flexible for our needs. So we have to add a new file called functions.php inside the forum plugin root folder and include this file inside the includes function of the WPWAF_Forum class.

Now we can add the custom email sending function to the functions.php file, as shown in the following code:

    function wpwaf_send_user_notification ($user_id, $plaintext_pass = '',     $activate_code = '') { 
$user = new WP_User($user_id);
$user_login = stripslashes($user->user_login);
$user_email = stripslashes($user->user_email);

$message = sprintf(__('New user registration on %s:','wpwaf'), get_option('blogname')) . '\r\n\r\n';
$message .= sprintf(__('Username: %s','wpwaf'), $user_login) . '\r\n\r\n';
$message .= sprintf(__('E-mail: %s','wpwaf'), $user_email) . '\r\n';

@wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration','wpwaf'), get_option('blogname')), $message);

if (empty($plaintext_pass))
return;

$act_link = site_url() . "/user/activate/? wpwaf_activation_code=$activate_code";

$message = __('Hi there,','wpwaf') . '\r\n\r\n';
$message .= sprintf(__('Welcome to %s! Please activate your account using the link:','wpwa'), get_option('blogname')) . '\r\n\r\n';
$message .= sprintf(__('<a href="%s">%s</a>','wpwaf'), $act_link, $act_link) . '\r\n';
$message .= sprintf(__('Username: %s','wpwaf'), $user_login) . '\r\n';
$message .= sprintf(__('Password: %s','wpwaf'), $plaintext_pass) . '\r\n\r\n';

wp_mail($user_email, sprintf(__('[%s] Your username and password','wpwa'), get_option('blogname')), $message);

}

The first part of this function, up to the first wp_mail execution, prepares the e-mail to be sent to the administrator with the username and e-mail of the user. The next part contains the e-mail sending functionality for the registered user. The e-mail sent to the user needs to be customized to include the activation link. Therefore, we take the activation parameter passed to the function add it to the predefined activation link. Next, we send the e-mail with the username, password, and activation link.

That's about all we need to change from the original function. Finally, we set the success message to be passed into the login screen.

Now, let's move back to the register_user function. Once the notification is sent, the registration process is completed and the user will be redirected to the login screen. Once the user has the e-mail in their inbox, they can use the activation link to activate the account.