Wordpress to generate WP_Error Object for user log

2019-09-03 17:42发布

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条回答
该账号已被封号
2楼-- · 2019-09-03 17:55

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;
}
查看更多
登录 后发表回答