因为我必须在不同类中处理多个不同代码区域的错误,所以我在身份验证处理程序中出现了一个全局错误:
function json_myauthname_auth_handler( $user = false) {
global $wp_json_myauthname_auth_error;
$wp_json_myauthname_auth_error = null;
...
}
然后,如果在任何地方发生错误,我会设置相应的错误代码和消息:
$wp_json_myauthname_auth_error = new WP_Error( \'json_login_failed\', __( "We didn\'t recognize your email address or password. Please try again." ), array( \'status\' => 401 ) );
然后,您只需要一个过滤器来处理身份验证错误的检查
// Error handling
function json_myauthname_auth_error( $error ) {
// Passthrough other errors
if ( ! empty( $error ) ) {
return $error;
}
}
add_filter( \'json_authentication_errors\', \'json_myauthname_auth_error\' );
如果不需要所有这些,可以返回相应的错误:
return new WP_Error( \'json_login_failed\', __( "We didn\'t recognize your email address or password. Please try again." ), array( \'status\' => 401 ) );