简体   繁体   中英

redirect the page if a user login was empty at wordpress?

I have no problem to redirect the page if a user enter the wrong username or password with the following code. However, if a user left either the username or password empty, the redirect function fail, it will go to wordpress/login.php. I want to redirect the page if the user didn't enter anything but hit the submit button.

 <?php add_action( 'wp_login_failed', 'my_front_end_login_fail' ); // hook failed login function my_front_end_login_fail( $username ) { $referrer = $_SERVER['HTTP_REFERER']; // where did the post submission come from? // if there's a valid referrer, and it's not the default log-in screen if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') ) { wp_redirect( $referrer . '?login=failed' ); // let's append some information (login=failed) to the URL for the theme to use exit; } } ?> 

You can use the 'wp_authenticate' action, this is one of the earliest action in the login process:

add_action( 'wp_authenticate', '_catch_empty_user', 1, 2 );

function _catch_empty_user( $username, $pwd ) {
 if ( empty( $username ) ) {
   wp_safe_redirect( $redirect_to_whereever_you_want );
   exit();
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM