Wordpress to generate WP_Error Object for user log

2019-09-03 17:29发布

问题:

I want to return the Login Error Message even user key-in their privileges correctly. In other words, to terminate the login and show the error message.

When we key-in wrong passwords, wordpress shows:

There was an error authenticating your details.

ERROR: The password you entered for the username admin is incorrect. Lost your password?

.. on the login page. This is because of WP_Error Object is returned.

So my curious question is:

  • How to get/generate this WP_Error Object on my own, to return back? Is it Array?

回答1:

You need to hook into the authenticate wordpress hook. Then return a new WP_Error object to generate an error message and redirect back to the login page. Here is an example.

add_filter('authenticate', 'check_login_submit', 40, 3);

function check_login_submit($user, $username, $password) {
    $WP_Error = new WP_Error();
    $WP_Error->add('my_error', '<strong>Error</strong>: Something went wrong.');
    return $WP_Error;
}