admin管理员组

文章数量:1122832

I am modifying my wp-login.php but from within functions.php (in order not to hack the core).

I need to disable one WP_error with code: authentication_failed.

if i hack the core and modify wp-login.php at the code:

foreach ( $wp_error->get_error_codes() as $code ) {
    $severity = $wp_error->get_error_data( $code );
    foreach ( $wp_error->get_error_messages( $code ) as $error_message ) {
            if ( 'message' == $severity )
                $messages .= '  ' . $error_message . "<br />\n";
            else
                $errors .= '    ' . $error_message . "<br />\n";
        }
}

to:

foreach ( $wp_error->get_error_codes() as $code ) {
        $severity = $wp_error->get_error_data( $code );
        if($code !=='authentication_failed'){ <======= ADDED
            foreach ( $wp_error->get_error_messages( $code ) as $error_message ) {
                  if ( 'message' == $severity )
                        $messages .= '  ' . $error_message . "<br />\n";
                  else
                        $errors .= '    ' . $error_message . "<br />\n";
            }
         }
}

It works. But I don't want to hack the core, is there a way to disable it from functions.php?

I am modifying my wp-login.php but from within functions.php (in order not to hack the core).

I need to disable one WP_error with code: authentication_failed.

if i hack the core and modify wp-login.php at the code:

foreach ( $wp_error->get_error_codes() as $code ) {
    $severity = $wp_error->get_error_data( $code );
    foreach ( $wp_error->get_error_messages( $code ) as $error_message ) {
            if ( 'message' == $severity )
                $messages .= '  ' . $error_message . "<br />\n";
            else
                $errors .= '    ' . $error_message . "<br />\n";
        }
}

to:

foreach ( $wp_error->get_error_codes() as $code ) {
        $severity = $wp_error->get_error_data( $code );
        if($code !=='authentication_failed'){ <======= ADDED
            foreach ( $wp_error->get_error_messages( $code ) as $error_message ) {
                  if ( 'message' == $severity )
                        $messages .= '  ' . $error_message . "<br />\n";
                  else
                        $errors .= '    ' . $error_message . "<br />\n";
            }
         }
}

It works. But I don't want to hack the core, is there a way to disable it from functions.php?

Share Improve this question asked Mar 16, 2015 at 20:06 ccotccot 1493 silver badges12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can use the pluggable function wp_authenticate to override the core.

https://developer.wordpress.org/reference/functions/wp_authenticate/

Important caution: Test this code on a sandbox before deploying live!

function wp_authenticate($username, $password) {
    $username = sanitize_user($username);
    $password = trim($password);

    $user = apply_filters( 'authenticate', null, $username, $password );

    if ( $user == null ) {
        $user = new WP_Error( 'authentication_failed', __( '<strong>ERROR</strong>: Invalid username or incorrect password.' ) );
    }

    $ignore_codes = array( 'authentication_failed' );

    if ( is_wp_error( $user ) && !in_array( $user->get_error_code(), $ignore_codes ) ) {
        do_action( 'wp_login_failed', $username );
    }

    return $user;
}
add_action( 'init', 'wp_authenticate' );

本文标签: functionsdisable WPerror authenticationfailed