行动wp_login_failed
当由于错误的用户名/密码组合导致登录失败时激发。所以这是一个很好的开始。这是一个超级简单的示例,只需重定向到主页。
<?php
add_action( \'wp_login_failed\', \'wpse25628_login_failed\', 10, 1 );
/**
* Catches all failed logins and redirect them to the
* websites home page.
*/
function wpse25628_login_failed( $username )
{
/*
May want to do something here to give the user feedback
for instance call `global $user;`, which will be a
wp_error object, you could append the code as a
$_GET variable and adjust your front end according to the
error message
*/
wp_redirect(
home_url(),
302
);
exit();
}
当然,还有
wp_logout
还有钩子!也许你在这里给人们发一个感谢页面,而不是主页。
<?php
add_action( \'wp_logout\', \'wpse25628_catch_logout\' );
/**
* When a user logs out, send them back to the home page
*/
function wpse25628_catch_logout()
{
wp_redirect(
home_url(),
302
);
exit();
}
不幸的是
wp_login_failed
如果用户名或密码为空,则不会触发钩子。同样令人遗憾的是,当发生这种情况时,缺少被触发的钩子。使用javascript在客户端验证这类内容可能更好。这里有一个简单的例子。
jQuery(document).ready(function(){
jQuery(\'#loginform\').submit(function(e){
var errors;
jQuery(\'#loginform input\').each(function(){
var val = jQuery(this).val()
if( ! val )
{
errors = true;
}
});
if( errors )
{
e.preventDefault();
}
});
});