Chặn username hoặc email đăng nhập vào website

Chặn username hoặc email đăng ký hoặc đăng nhập vào website của bạn sẽ khá hữu ích cho các website mở cho phép nhiều thành viên đăng ký, khi thực hiện giới hạn bớt user ảo, user từ Nga hoặc dạng user tạo tự động hàng loạt.

Ví dụ mình luôn chặn các user dạng như

  • test
  • blogspot
  • wintds
  • se*
  • spam
  • admin
  • root

Chặn username hoặc email đăng nhập hoặc đăng ký vào website WordPress , bạn có thể sử dụng code dưới đây và thêm vào file functions.php và thêm user hoặc email để chặn là được.

// Hook into the registration process
add_action('registration_errors', 'vutruso_restrict_registration_username_email', 10, 3);
add_filter('registration_errors', 'vutruso_restrict_registration_email', 10, 3);

// Validate the username and email during registration
function vutruso_restrict_registration_username_email($errors, $sanitized_user_login, $user_email) {
    // Define the list of restricted usernames and emails
    $restricted_usernames = array('admin', 'superuser', 'root');
    $restricted_emails = array('example@example.com', 'test@test.com');

    // Check if the username is restricted
    if (in_array(strtolower($sanitized_user_login), $restricted_usernames)) {
        $errors->add('username_invalid', __('The username you entered is not allowed.'));
    }

    // Check if the email address is restricted
    if (in_array(strtolower($user_email), $restricted_emails)) {
        $errors->add('email_invalid', __('The email address you entered is not allowed.'));
    }

    return $errors;
}

// Validate the email during registration
function vutruso_restrict_registration_email($errors, $sanitized_user_login, $user_email) {
    // Check if the email address is already registered
    if (email_exists($user_email)) {
        $errors->add('email_exists', __('The email address you entered is already registered.'));
    }

    return $errors;
}

Bạn để ý đến 2 vị trí này để thay đổi user và email bị chặn là được nhé

$restricted_usernames = array('admin', 'superuser', 'root');
$restricted_emails = array('example@example.com', 'test@test.com');

The email address you entered is not allowed

Nếu bạn muốn sử dụng plugin thì có thể search plugin Restrict Usernames Emails Characters của tác giả này nhé https://wordpress.org/plugins/restrict-usernames-emails-characters/

Nếu bạn thấy bài viết có ích hãy sao chép link và chia sẻ bài viết
daotiendung

Tiến Dũng Đào chuyên quản lý, vận hành các dịch vụ website. Anh có nhiều năm kinh nghiệm về VPS, Hosting, technical SEO, CMS. Đặc biệt yêu thích WordPress với hơn 5 năm phát triển theme và plugin. Sở thích của anh là đọc, viết blog, đi du lịch, tập võ và chia sẻ các kiến thức cho mọi người.

Bài viết liên quan