WordPress 用户登录失败(账号或密码错误)重定向到自定义页面

如果我们使用前台自定义的登录页面,一般都不希望用户登录失败(账号密码错误)后跳转到WP后台默认的登录表单,那就需要做一个重定向,让用户登录失败后重定向到我们自定义的页面。

将下面的代码添加到主题的 functions.php 即可:

/**
 * WordPress 用户登录失败重定向到自定义页面
 * https://www.wpdaxue.com/redirect-wordpress-failed-logins.html
 */
add_action('wp_login_failed', 'my_front_end_login_fail');
function my_front_end_login_fail($username){
	// 获取提交数据的来源页面
	$referrer = $_SERVER['HTTP_REFERER'];
	// 如果是一个有效的来源,并且不是默认登录页面或后台管理页面
	if(!empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin')){
		// 追加一些信息(login=failed)到链接中,让主题使用
		wp_redirect($referrer . '?login=failed');
		exit;
	}
}

假设你的自定义登录页面地址为 http://你的域名/login,上面的第 8 行就会获取到这个页面链接,然后登录失败后,按照第 12 行的设置,就会返回 http://你的域名/login?login=failed 这个链接。你可以通过修改12行 wp_redirect() 这个函数的参数跳转到任何你想要的地址。

来源:

https://www.wpdaxue.com/redirect-wordpress-failed-logins.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_33152.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?