Chuyên mục
Wordpress

Form đăng ký tài khoản trong WordPress – không dùng plugin

Bạn có thể tạo một Form đăng ký tài khoản trong Wordpress mà không cần dùng một plugin nào. Chỉ vài dòng code PHP, thêm một ít CSS và Javascript, đủ cho nhu cầu của chính mình.

Không thể phủ nhận lợi ích của các plugin tạo Form đăng ký tài khoản hỗ trợ sẵn trong thư viện plugin của WordPress. Thật sự tiết kiệm thời gian mà còn hưởng được sự bảo mật bởi các nhân viên thiết kế Worpress chuyên nghiệp.

Tuy nhiên, các plugin có sẵn thông thường tạo ra khá nhiều CSS và Java (để phục vụ nhiều đối tượng sử dụng khác nhau). Điều này sẽ ảnh hưởng chút ít đến tốc độ website của bạn. Và việc thêm nhiều Plugin khác nữa, chắc chắc sự ảnh hưởng là khác lớn!

Bạn có thể tạo một Form đăng ký tài khoản trong WordPress mà không cần dùng một plugin nào. Chỉ vài dòng code PHP, thêm một ít CSS và Javascript, đủ cho nhu cầu của chính mình.

Việc giảm thiểu CSS và Javascript sẽ có giá trị rất lớn đến tốc độ website. Đặc biệt, các bạn sẽ thấy rõ điều này qua việc kiểm tra bằng Google Pagespeed Insights.

Bắt đầu tạo Form đăng ký tài khoản trong WordPress

Form đăng ký tài khoản trong WordPress – không dùng plugin
Form đăng ký tài khoản trong WordPress – không dùng plugin

Đây là cách mà tôi đã thực hiện với các website có sử dụng WordPress của mình. Tất nhiên, bạn có thể làm khác và tốt hơn!

  • Form đăng ký tài khoản được đặt trong template part, tiện cho việc tạo ra nhiều trang Register khác nhau.
  • Tại trang Đăng ký tài khoản, sử dụng hàm get_template_part() để nhúng form ở trên.

File /[MY_THEME]/template_part/register_form.php

/**
* Ví dụ một Form đăng ký tài khoản trong WordPress,  
* không dùng plugin
**/
<?php
if (! defined('ABSPATH')) {
    exit;
}
if ( is_user_logged_in() ) {
	return;
}

if (isset($_POST['register']))
{
    $username               = strtolower($_POST['useremail']);
    $useremail              = $username;
    $password               = $_POST['password'];
    $password_confirmation  = $_POST['password_confirmation'];
    $errors = array();
    if(empty( $username ) || empty( $useremail ) || empty($password))
    {
        $errors[] =  __('Required form field is missing!', 'language_domain');
    } else {
    
        if ( !is_email( $useremail ) )
        {
            $errors[] =  __('Email is not valid!','language_domain');
        }
        if ( username_exists( $username ) || email_exists( $useremail ))
        {
            $errors[] =  __('Email Already exist!','language_domain');
        }
        
        if ( 8 > strlen( $password ) ) {
            $errors[] =  __('Password must be at least 8 characters!','language_domain');
        }
        
        if (0 !== strcmp($password, $password_confirmation))  {
            $errors[] =  __('Passwords do not match!','language_domain');
        }
    }
    
    if (empty( $errors ))
    { 
        $userdata = array(
            'user_login'    =>   $username,
            'user_email'    =>   $useremail,
            'user_pass'     => $password
            );
            
        $userResult = wp_insert_user( $userdata );
        if(is_numeric($userResult)) {
            $redirect_to = wp_login_url();
            if(isset($_GET['redirect_to']) && !empty($_GET['redirect_to']))
                $redirect_to = $_GET['redirect_to'];
            $strSuccess = __('Success! Thank you for register!', 'language_domain').'<script>setTimeout(function(){window.location="'.$redirect_to.'"},3000);</script>';
        } else {
            $errors[] = $userResult;
        }
    }
    
    if (!empty( $errors ))
    { 
        $strError = '<p class="title">'.__('ERROR(s)', 'language_domain').'</p>';
        foreach ( $errors as $error )
        {
             $strError.='<p>'.$error.'</p>';
        }
    } 
}
?>

<h1><?php echo __('Register account', 'language_domain'); ?></h1>
<?php if(isset($strError)){echo '<div class="error-dialog show">'.$strError.'</div>';}?>
<?php if(isset($strSuccess)){echo '<div class="success-dialog show">'.$strSuccess.'</div>';}?>
<form action="" method="post" name="user_registeration">
    <label><?php echo __('Email address', 'language_domain'); ?> <span class="error">*</span></label>
    <input type="text" name="useremail" class="text" value="<?php echo (isset($useremail)) ? $useremail : '' ?>" placeholder="<?php echo __('Enter Your Email', 'language_domain'); ?>" required /> <br />
    <label><?php echo __('Password', 'language_domain'); ?> <span class="error">*</span></label>
    <input type="password" name="password" class="text" value="<?php echo (isset($password)) ? $password : '' ?>" placeholder="<?php echo __('Enter Your password', 'language_domain'); ?>" required /><br />
    <label><?php echo __('Confirm password', 'language_domain'); ?> <span class="error">*</span></label>
    <input type="password" name="password_confirmation" class="text" placeholder="<?php echo __('Confirm Your password', 'language_domain'); ?>" required /> <br />
    <input type="submit" name="register" value="<?php echo __('SignUp', 'language_domain');?>" />
</form>

File /[MY_THEME]/templates/register.php

<?php
/**
 * Template Name: Register Template 
 **/

if ( is_user_logged_in() ) {
    $redirect_url = '/my-account/';
    if(strlen($_GET['redirect_to']) > 0)
        $redirect_url = $_GET['redirect_to'];
    wp_safe_redirect($redirect_url);
    exit;
}
get_header();
?>
<div class="container register">

<?php
get_template_part( '/template-parts/register','form' );
?>

</div>
<?php
get_footer();

Mở rộng

  • Bạn cần thêm CSS cho Form đăng ký tài khoản của bạn
  • Các hàm __(‘text’, ‘domain’) có thể thay thế bằng ‘text’ nếu cần dùng đa ngôn ngữ
  • Form đăng nhập này đã sử dụng Email làm Username, bạn có thể tách nó ra thành 2 ô nhập riêng biệt.
  • Có thể thêm chức năng yêu cầu độ khó của mật khẩu (ví dụ: phải bao gồm ký tự và số, phải có ký tự đặc biệt, phải có ký tự hoa, …)

2 trả lời trong “Form đăng ký tài khoản trong WordPress – không dùng plugin”

Trả lời