As of WordPress 4.4, 行动lostpassword_post
通过$errors
对象:
function wpse_185243_lastpassword_post( $errors ) {
if ( ! $captcha_valid /* The result of your captcha */ ) {
$errors->add( \'invalid_captcha\', \'<strong>ERROR:</strong> Try again sonny.\' );
}
}
add_action( \'lostpassword_post\', \'wpse_185243_lastpassword_post\' );
<小时>
Pre 4.4 legacy answer
这是您所指的相关代码(
retrieve_password()
在里面
wp-login.php
):
function retrieve_password() {
$errors = new WP_Error();
// Some error checking
do_action( \'lostpassword_post\' );
if ( $errors->get_error_code() )
return $errors;
// Some more code
/**
* Filter whether to allow a password to be reset.
*
* @since 2.7.0
*
* @param bool true Whether to allow the password to be reset. Default true.
* @param int $user_data->ID The ID of the user attempting to reset a password.
*/
$allow = apply_filters( \'allow_password_reset\', true, $user_data->ID );
if ( ! $allow )
return new WP_Error(\'no_password_reset\', __(\'Password reset is not allowed for this user\'));
else if ( is_wp_error($allow) )
return $allow;
}
正如你所说,没有办法
$errors
(这是一个局部变量,永远不会传递给筛选器/操作-可能值得在trac票证中提交功能请求)。
但是,您似乎可以使用allow_password_reset
返回新的WP_Error
, WordPress将以与核心错误相同的方式处理它:
function wpse_185243_lost_password_captcha( $result, $user_id ) {
if ( ! $captcha_valid /* The result of your captcha */ ) {
$result = new WP_Error( \'invalid_captcha\', \'<strong>ERROR:</strong> Try again sonny.\' );
}
return $result;
}
add_filter( \'allow_password_reset\', \'wpse_185243_lost_password_captcha\', 10, 2 );