add_action( \'wp_authenticate\', \'my_front_end_login_fail\', 1, 2 );
function my_front_end_login_fail( $user, $pwd ) {
}
你注意到钩子了吗
wp_authenticate
接受
two 争论?你知道吗,你必须用
add_action()
呼叫
您应该不时查看核心文件:
$ignore_codes = array(\'empty_username\', \'empty_password\');
if (is_wp_error($user) && !in_array($user->get_error_code(), $ignore_codes) ) {
do_action(\'wp_login_failed\', $username);
}
The
wp_login_failed
仅当用户名或userpassword为
not 空的
所以让我们从头开始,清理一些东西:
add_action( \'wp_authenticate\', \'my_front_end_login_fail\', 1, 2 );
add_action( \'wp_login_failed\', \'my_front_end_login_fail\', 1, 1 );
function my_front_end_login_fail( $user, $pwd = \'\' ) {
// reasons to stop here
// $user is not empty
// $user is not a wp-error object
// $pwd is not empty
if ( ! empty( $user ) && ! empty( $pwd ) && ! is_wp_error( $user ) )
return false;
// if a referer is set, use it. else setup the standard login file
$referrer = ( isset( $_SERVER[\'HTTP_REFERER\'] ) && ! empty( $_SERVER[\'HTTP_REFERER\'] ) ) ?
$_SERVER[\'HTTP_REFERER\'] : home_url( \'wp-login.php\' ); // take the safe one, use home_url()
/*
* since PHP5 we can parse an url
* @see http://php.net/manual/en/function.parse-url.php
*
* parse_url( \'http://www.example.com/wp-login.php?login=failed&foo=bar\' ) gives us something like that:
*
* array (
* \'scheme\' => \'http\'
* \'host\' => \'www.example.com\'
* \'path\' => \'/wp-login.php\'
* \'query\' => \'login=failed&foo=bar\'
* )
*/
$parsed_url = parse_url( $referrer );
/*
* Another fine function is parse_str()
* @see: http://php.net/manual/en/function.parse-str.php
*
* parse( \'login=failed&foo=bar\', $query ); results in
* array(
* \'login\' => \'failed\'
* \'foo\' => \'bar\'
* )
*
*/
parse_str( $parsed_url[\'query\'], $query );
// if there\'s a valid referrer, and it\'s not the default log-in screen
if ( ! strstr( $parsed_url[\'path\'], \'wp-login\' ) && ! strstr( $parsed_url[\'path\'], \'wp-admin\' ) ) {
// already has the failed don\'t appened it again
$redirect_to = $referrer;
if( ! isset( $query[\'login\'] ) || \'failed\' !== $query[\'login\'] ) {
// add the failed
// but never ever use a simple string concaternation
// what will result if the referer is \'example.com?foo=bar\'?
// it will result in \'example.com?foo=bar?login=failed\' OUTCH!
$redirect_to = add_query_arg( array( \'login\' => \'failed_empty\' ), $referrer );
}
// you don\'t want to redirect to google or somewhere else, you want to redirect to your
// own domain. so use wp_safe_redirect()
wp_safe_redirect( $redirect_to );
exit;
}
}
这有点棘手。我们将函数挂钩到两个不同的操作中。第一个动作通过
two 参数,仅第二个
one. 我们的回访总是需要
two 参数,但如果只传递一个参数,则第二个参数将使用空字符串预定义。
注释中解释了该函数的其余部分。