0

I've added an extra field to the login form. When trying to get the user's value of that field(before checking if it's equal) I have a get_user_by('login', $username);. $username being an email adres(the only thing I'll allow.)

The specific error is: Warning: trim() expects parameter 1 to be string, object given in /(removed for privacy)/public_html/wp-includes/class-wp-user.php on line 201

Full code: (After click on login on /wp-login)

add_filter('wp_authenticate_user', 'filter_login_auth', 10, 3);
function filter_login_auth($username, $password) {
    //get posted value
    $value = $_POST['klantnr'];

    //get user object
    $user = get_user_by('login', $username);

    //get stored klantnr
    $stored = get_user_meta($user->ID, 'klantnr',  true);

    if(!$user || empty($value) || $value != $stored) {
        //No user, empty, or not correct
        remove_action('authenticate', 'wp_authenticate_username_password', 20);

        //Create error to return
        $user = new WP_Error('denied', __("<strong>ERROR</strong>: Je klantnummer is niet correct."));
    }

    // return null so if klantnr is correct the script continues.
    return $user;
}
2
  • Can you post the code where you assign the vlue to $username varible?
    – cybmeta
    Commented Apr 29, 2016 at 13:27
  • Has been added.
    – Evergetic
    Commented Apr 29, 2016 at 13:29

1 Answer 1

0

If your code, you use the first argument passed to wp_authenticate_user filter callback. That is a WP_User object, it not a email address and it is not a string.

You may be confusing because of the name of the variable, $username, I suggest to change the name to $user, whcih is more appropiate and it is a better representation of the real value of that variable.

add_filter('wp_authenticate_user', 'filter_login_auth', 10, 3);
function filter_login_auth( $user, $password ) {
    //get posted value
    $value = $_POST['klantnr'];

    //get user object
    $user = get_user_by('login', $user->user_login);

    // .....

}

But you don't need that because get_user_by() returns a user object and you already have it:

add_filter('wp_authenticate_user', 'filter_login_auth', 10, 3);
function filter_login_auth( $user, $password ) {
    //get posted value
    $value = $_POST['klantnr'];

    // You don't need this, you already have the user object
    // get user object
    //$user = get_user_by('login', $user->user_login);

    // .....

}

For more information, see wp_authenticate_user docs.

2
  • EDIT: didn't see your edit. going to try it out! I'll let you know
    – Evergetic
    Commented Apr 29, 2016 at 13:46
  • 1
    It works! //Marked as answer
    – Evergetic
    Commented Apr 29, 2016 at 13:48

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.