Home WordpressWordPress cơ bản Cách đăng nhập bằng email cho tài khoản wordpress

Cách đăng nhập bằng email cho tài khoản wordpress

by admincp

– Thông thường wordpress đăng nhập bằng username, đôi khi bạn muốn người dùng của bạn đăng nhập bằng email. Bằng cách loại bỏ cách authenticate thông thường và thêm luật filter authenticate mới, xác thực user_login bởi email người đăng nhập cung cấp. Xem code dưới đây.

// remove the default filter
remove_filter('authenticate', 'wp_authenticate_username_password', 20, 3);
//custom authenticate, vd: đăng nhập user bởi email
add_filter('authenticate', 'fb_authenticate_username_password', 20, 3);
function fb_authenticate_username_password($user, $username, $password)
{
	// If an email address is entered in the username box, 
	// then look up the matching username and authenticate as per normal, using that.
	if (!empty($username))
		$user=get_user_by('email', $username);	//lấy user
 
	if (isset($user->user_login, $user))
		$username=$user->user_login;
 
	//print_r($user);
	// using the username found when looking up via email
	return wp_authenticate_username_password(NULL, $username, $password);
}

Hãy cho mình biết suy nghĩ của bạn trong phần bình luận bên dưới bài viết này. Hãy theo dõi kênh chia sẻ kiến thức WordPress của Vinastar trên TwitterFacebook

Liên hệ

Công ty chuyên Thiết kế website uy tín nhất Miền Bắc: http://vinastar.net

Hotline tư vấn: 0989 48 3456

Nguồn: Sưu tầm trên internet

You may also like

Leave a Comment