Thứ Ba, 24/06/2025
Loading...
  • Login
Hoàng Giang Nam Blog
  • Home
  • WordPress
    • WordPress
    • Woocommerce
  • Windows
  • Office
  • Phần Mềm
  • Thủ thuật
  • Linux
    • Ubuntu
    • CentOS
    • VPS – Server
    • Web Server
No Result
View All Result
Hoàng Giang Nam Blog
  • Home
  • WordPress
    • WordPress
    • Woocommerce
  • Windows
  • Office
  • Phần Mềm
  • Thủ thuật
  • Linux
    • Ubuntu
    • CentOS
    • VPS – Server
    • Web Server
No Result
View All Result
  • Login
Hoàng Giang Nam Blog
No Result
View All Result
Home Wordpress

Gửi cho người dùng WordPress một liên kết để đăng nhập bằng email thay vì sử dụng mật khẩu

admin by admin
01/01/2025
Share on FacebookShare on Twitter

Tất nhiên, chúng tôi sẽ không thực hiện bất kỳ thay đổi nào trong tệp wp-login.php của WordPress, nhưng do thiếu hook bên trong nó, tốt hơn là bạn nên tạo một mẫu trang tùy chỉnh.

Tôi quyết định đặt tất cả mã vào một tệp duy nhất, nhưng bạn có thể tách nó thành các tệp khác nhau.

/* Template name: No more passwords */

// Redirect logged-in users to member's area or admin area
if ( is_user_logged_in() ) {
    wp_redirect( admin_url() ); 
    exit;
}

// Check if the form is being submitted
if ( !empty( $_POST['misha_email'] ) && ( $user_data = get_user_by( 'email', $_POST['misha_email'] ) ) && !is_wp_error( $key = get_password_reset_key( $user_data ) ) ) {
    // Generate login URL
    $login_url = add_query_arg( array( 'key' => $key, 'email' => rawurlencode( $user_data->user_email ) ), site_url('login') );

    // Prepare email headers and message
    $headers = array( 
        'From: Misha <[email protected]>',
        'Content-type: text/html; charset=utf-8'
    );
    $message = 'Here is your link to login <a href="' . $login_url . '">' . $login_url . '</a>';

    // Send email
    wp_mail( $user_data->user_email, 'Sign in to YOUR-WEBSITE.COM', $message, $headers );

    // Redirect user to success message page
    wp_redirect( add_query_arg('status', 'sent', site_url('login') ) );
    exit;
} 

// If it's the login link being clicked
else if( !empty( $_GET['key'] ) && !empty($_GET['email']) ) {
    // Validate the reset key
    if( ( $user_data = get_user_by( 'email', $_GET['email'] ) ) && ( !is_wp_error( $check = check_password_reset_key( wp_unslash( $_GET['key']), $user_data->user_login ) ) ) ) {
        // Set authentication cookie
        wp_set_auth_cookie( $check->data->ID, true, false );
        
        // Redirect user to admin area or members' dashboard
        wp_redirect( admin_url() ); 
        exit;
    } else {
        echo 'The authentication link is expired or incorrect. Please try again.';
    }
}

// Display status message after email is sent
if( isset( $_GET['status'] ) && $_GET['status'] == 'sent' ) {
    echo 'If this email is registered on the website, please check your inbox.';
}

// Display the email form
echo '<form action="" method="POST">
    <input type="email" name="misha_email" required />
    <button type="submit">Send login link</button>
</form>';

Và điều cuối cùng tôi muốn bạn biết là bạn có thể thay đổi thời gian hết hạn liên kết bằng password_reset_expirationaction hook (mặc định là 1 ngày)

add_filter( 'password_reset_expiration', 'misha_custom_activation_link_period', 9999 );

function misha_custom_activation_link_period( $day_in_seconds ) {
    return 900; // 15 minutes
}

Hy vọng sẽ hữu ích với bạn.

admin

admin

Next Post
HuananZhi X79 / X99 Motherboard BIOS POST codes

HuananZhi X79 / X99 Motherboard BIOS POST codes

Please login to join discussion

Recommended.

Xóa, Sửa, Nâng dung lượng RAM ẢO – Swap trên Ubuntu

Xóa, Sửa, Nâng dung lượng RAM ẢO – Swap trên Ubuntu

01/01/2025
Cách gia hạn Windown Server hết hạn

Cách gia hạn Windown Server hết hạn

31/12/2024

Trending.

HuananZhi X79 / X99 Motherboard BIOS POST codes

HuananZhi X79 / X99 Motherboard BIOS POST codes

01/01/2025
Tổng hợp download link ISO Windows

Tổng hợp download link ISO Windows

31/12/2024
Cài đặt AApanel & TOR network

Cài đặt AApanel & TOR network

01/01/2025
Thay đổi port SSH của VPS

Thay đổi port SSH của VPS

19/12/2024
Tạo file IMG Windows.GZ bằng VM VirtualBox để cài đặt trên VPS Digitalocean, Linode

Tạo file IMG Windows.GZ bằng VM VirtualBox để cài đặt trên VPS Digitalocean, Linode

31/12/2024
Hoàng Giang Nam

Blog chia sẽ kiến thức lập trình, source code, themes, plugin, phần mềm & thủ thuật máy tính...

Follow Us

Danh mục

  • CentOS
  • Chung
  • Server Email
  • Thủ thuật máy tính
  • Ubuntu
  • VPS – Server
  • Web Server
  • Windows
  • Woocommerce
  • Wordpress

Recent News

Disable Web Search Results in Windows 10/11 Start Menu

16/01/2025
Hướng dẫn lấy Client ID + Client Secret + Refresh Token Google Drive API

Hướng dẫn lấy Client ID + Client Secret + Refresh Token Google Drive API

19/12/2024
  • About
  • Advertise
  • Privacy & Policy
  • Contact

© 2024 Hoanggiangnam.com

No Result
View All Result
  • Home
  • Phần Mềm
  • Thủ thuật máy tính
  • WordPress
  • Ubuntu
  • Office
  • Photography
  • Windows

© 2024 Hoanggiangnam.com

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Click to Copy