The wordpress user name can be logged on via email

Source: Internet
Author: User

There are two methods,

Of course, I must only use the code ~

Add the following code to 'functions. Php ':

The code is as follows: Copy code

Function email_address_login ($ username ){
$ User = get_user_by_email ($ username );
If (! Empty ($ user-> user_login ))
$ Username = $ user-> user_login;
Return $ username;
}
Add_action ('WP _ authenticate ', 'Email _ address_login ');

This is all done.

However, the prompt text on the wp login page indicates the user name.

At the same time, you have to let the registered users of your website know that you now support email login.

Open 'WP-login. Php'

Search Code

The code is as follows: Copy code

<P>
<Label for = "user_login"> <? Php _ e ('Username')?> <Br/>
<Input type = "text" name = "log" id = "user_login" class = "input" value = "<? Php echo esc_attr ($ user_login);?> "Size =" 20 "tabindex =" 10 "/> </label>
</P> www.111cn.net

Modify it

<P>
<Label for = "user_login"> <? Php _ e ('user name or email address')?> <Br/>
<Input type = "text" name = "log" id = "user_login" class = "input" value = "<? Php echo esc_attr ($ user_login);?> "Size =" 20 "tabindex =" 10 "/> </label>
</P>

You are done.

The only drawback is that you will overwrite the modified 'WP-login. Php' file after upgrading.

Well. The above practice can be replaced by a piece of code.

PHP

The code is as follows: Copy code

// Modify the WordPress user name filtering mechanism and obtain the user name through Email
Function ludou_allow_email_login ($ username, $ raw_username, $ strict ){
If (filter_var ($ raw_username, FILTER_VALIDATE_EMAIL )){
$ User_data = get_user_by ('email ', $ raw_username );

If (empty ($ user_data ))
Wp_die (_ ('<strong> ERROR </strong>: There is no user registered with that email address.'), 'user name incorrect ');
Else
Return $ user_data-> user_login;
  }
Else {
Return $ username;
  }
}
// Modify the text on the logon interface. Change "user name" to "user name or email address"
Function ludou_change_text (){
Echo '<script type = "text/javascript">
Var user_login_node = document. getElementById ("user_login ");
Var old_username_text = user_login_node.parentNode.innerHTML;
User_login_node.parentNode.innerHTML = old_username_text.replace (/username/, "username or email ");
</Script> ';
}
If (in_array ($ GLOBALS ['pagenow'], array ('WP-login. Php') & strpos ($ _ SERVER ['request _ url'], '? Action = register ') === FALSE & strpos ($ _ SERVER ['request _ URI'], '? Action = lostpassword ') ===false & strpos ($ _ SERVER ['request _ URI'], '? Action = rp ') = FALSE ){
Add_filter ('sanitize _ user', 'ludou _ allow_email_login ', 10, 3 );
Add_action ('login _ footer ', 'ludou _ change_text ');
}

Similarly, add it to functions. php ~

Method 3 Add the following code to the functions. php file of the current topic.

The code is as follows: Copy code

// Allow WordPress to log on with a user name or email address
Function dr_email_login_authenticate ($ user, $ username, $ password ){
If (is_a ($ user, 'WP _ user '))
Return $ user;
 
If (! Empty ($ username )){
$ Username = str_replace ('&', '&', stripslashes ($ username ));
$ User = get_user_by ('email ', $ username );
If (isset ($ user, $ user-> user_login, $ user-> user_status) & 0 = (int) $ user-> user_status)
$ Username = $ user-> user_login;
 }
 
Return wp_authenticate_username_password (null, $ username, $ password );
}
Remove_filter ('authenticate', 'WP _ authenticate_username_password ', 20, 3 );
Add_filter ('authenticate', 'Dr _ email_login_authenticate ', 20, 3 );
 
// Replace "username" with "username/email"
Function username_or_email_login (){
If ('WP-login. Php '! = Basename ($ _ SERVER ['script _ name'])
Return;
 
?> <Script type = "text/javascript">
// Form Label
If (document. getElementById ('loginform '))
Document. getElementById ('loginform'). childNodes [1]. childNodes [1]. childNodes [0]. nodeValue = '<? Php echo esc_js (_ ('user name/mailbox ', 'Email-login');?> ';
 
// Error Messages
If (document. getElementById ('login _ error '))
Document. getElementById ('login _ error'). innerHTML = document. getElementById ('login _ error'). innerHTML. replace ('<? Php echo esc_js (_ ('Username');?> ',' <? Php echo esc_js (_ ('user name/mailbox ', 'Email-login');?> ');
</Script> <? Php
}
Add_action ('login _ form', 'username _ or_email_login ');

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.