I'm new to wordpress. I'm writing my first plugin. I don't understand how to have my function run only after submitting the login form while not running every time I refresh the login page. I tried with authenticate filter. But my function runs every time I refresh the login page.

I don't understand why authentication = accessing login page. I tried to default $username to null as argument, something like my_function($user, $username=null) and check if username is empty, if it is then don't do nothing. The problem is the function doesn't return any errors even if username is submitted.

This could be due to priority. I don't know if I did it correctly but the following didn't work. I tried to set highest priority.

add_filter('authenticate',array($myobject, 'login_authenticate'),1);

Could someone provide me with a sample function that returns custom error everytime the login form is submitted, even if login and password are correct.

2 Answers 2


The filter runs on every page load, but the first argument will be a WP_User object only on successful submission. It will be null if nothing was submitted, but most likely it will be a WP_Error object, which you can check for. If you want to know the specific error you have to look at the errors the object contains. The case where the form was not submitted will have both empty_username and empty_password error array keys.

This will possibly shed some light on things:

function myplugin_auth_signon( $user, $username, $password ) {
    var_dump( $user );
    return $user;
add_filter( 'authenticate', 'myplugin_auth_signon', 30, 3 );

Load the page, then try various combinations of incorrect and empty values, then a correct login.

  • I've tried something like this [code] public function auth($user, $username='') { if ( !empty($username) ) { return new WP_Error( 'broke', __( "custom error" ) ); } }[/code] It almost worked. However the problem is when I refresh the page, I get invalid username or password error. It could be because my function defaults $username to '', but I think an empty submitted field is '' as well
    – Kox
    Nov 4, 2014 at 19:54

I got it.

public function auth($user, $username='') {
    if ( !empty($username) ) {
        return new WP_Error( 'broke', __( "custom error" ) );
    else return $user;

It only returns error if username is submitted. A good base for my plugin. But I didn't know how to set priority so thank you for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.