Home  >  Article  >  CMS Tutorial  >  How to implement a WordPress login form without entering a password

How to implement a WordPress login form without entering a password

藏色散人
藏色散人forward
2021-05-19 14:17:162309browse

The following tutorial column of WordPress will introduce to you the WordPress login form that does not require entering a password. I hope it will be helpful to friends in need!

How to implement a WordPress login form without entering a password

WordPress login form without password

If you want to add a password-free login form to your WordPress theme, just enter the user A form that allows you to log in using your name or email can be implemented using the following method.

Copy the following code and paste it where you want to display the form

<form autocomplete="off" method="post">
<div class="form-group">
<input id="userlog" name="userlog" type="text" placeholder="<?php echo esc_attr__(&#39;Username/email&#39;, &#39;theme-domain&#39;); ?>" class="form-control" required="required">
</div>
<input type="hidden" name="action" value="my_login_action" />
<button type="submit"><?php echo esc_html__(&#39;Login&#39;, &#39;theme-domain&#39;); ?></button>  
</form>

Then, paste the following code into the theme function template functions.php:

add_action(&#39;init&#39;, function(){
// Return if not the login request
if( !isset( $_POST[&#39;action&#39;] ) || $_POST[&#39;action&#39;] !== &#39;my_login_action&#39; )
return;
$result;
$username = $_POST[&#39;userlog&#39;];
if ( ! $result ) {
$result = get_user_by( &#39;email&#39;, $username ); // user email
}
if ( ! $result ) {
$result = get_user_by(&#39;login&#39;, $username ); // user name
}
 
if ( !is_wp_error($result) ){
wp_clear_auth_cookie();
wp_set_current_user($result->ID);
wp_set_auth_cookie($result->ID);
wp_redirect( home_url( &#39;/&#39; ) );
exit;
}
die();
});

Only suitable for specific occasions Use, under normal circumstances if others know your login information, it is very risky to use this function...

The above is the detailed content of How to implement a WordPress login form without entering a password. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:zmingcx.com. If there is any infringement, please contact admin@php.cn delete