1

I'm developing a website where the username is a document number, called CPF (think of it as a national ID), which has the following mask:

000.000.000-00

I'm storing the usernames as plain numbers, but all our forms must have the mask above, which in turn makes it so the _POST['user_login'] always goes with the dash and dots. This is an example of a user's login/username:

$_POST['user_login']  = '123.456.789-00'
$actual_username_ondb = '12345678900'

This wasn't a problem for logging in, as I hook into the wp_authenticate action hook (not the pluggable function!) and I filter the username before continuing (and also check if the number is a valid CPF):

/**
 * Hooks before login and filters out the CPF mask if it exists
 */
add_action( 'wp_authenticate' , 'filter_username' );
function filter_username(&$username) {
    $cpf = preg_replace('/[^0-9]/is', '', $username);
    if (is_valid_cpf($cpf)) {
        $username = $cpf;
    }
}

However, now I'm dealing with the lost password form, and I couldn't find any way to do the same as above, i.e.: filter $_POST['user_login'] before going on with the password retrieval.

If there's any way to do it, it'd be via the hook lostpassword_post, because that's the earliest hook in the retrieve_password() function, but unfortunately it only triggers after the data is already parsed, so I don't know how it could be done.

1

Is this something that could work for you?

/**
* Hooks before login and filters out the CPF mask if it exists
*/
add_action( 'wp_authenticate' , 'somos_filter_username' );
function somos_filter_username() {
  $cpf = preg_replace('/[^0-9]/is', '', $_POST['user_login']);
  if (is_valid_cpf($cpf)) {
    return $cpf;
  }
}
| improve this answer | |
  • No, unfortunately, the action wp_authenticate is not called in when retrieving a lost password, otherwise it'd have worked out-of-the-box, since the login form already has the filter in place. – emi Jun 6 '18 at 12:11
1

you can try this hook.

function filter_retrieve_password_message( $title, $user_login, $user_data ) { 
    $cpf = preg_replace('/[^0-9]/is', '', $user_login);
    if (is_valid_cpf($cpf)) {
       $user_login = $cpf;
    }       
    return $title; 
}; 

// add the filter 
add_filter( 'retrieve_password_title', 'filter_retrieve_password_message', 10, 4 );

http://hookr.io/filters/retrieve_password_title/

Hope this will help you.

| improve this answer | |
  • This hooks much later on the retrieve_password() function. Also, the password reset e-mail has nothing to do with the issue, the function fails much earlier, as there's no user named 123.456.789-00 on the database, only 12345678900, so the function returns a WP_Error at line 337 of wp-login.php and it doesn't execute the rest. – emi Jun 6 '18 at 12:15
1

Another way could be to change the username via JavaScript, before the user send the lost password form.

function my_test_func()
{
    // load the script only on the lost password page
    if( array_key_exists('action', $_GET) AND $_GET['action'] == 'lostpassword' ):
?>  

<script type="text/javascript">

    // bind the function on the submit prozess
    document.querySelector('form').onsubmit = function(e) {

        // get the value from the input field
        var value = document.getElementById('user_login').value;

        //replace the first dot
        value = value.replace('.', '');

        //replace the second dot
        value = value.replace('.', '');

        //replace the dash
        value = value.replace('-', '');

        // write the value back in the input field
        document.getElementById('user_login').value = value;
    }

</script>

<?php
  endif;
}

add_action('login_footer', 'my_test_func');

The user enters his username and hit the button "Get New Password"

Wordpress Login Page with the unformatted username

The JavaScript change the username and the form will submit. Wordpress Login Page with the formatted username

| improve this answer | |
  • Yeah! That's actually exactly what I'm doing, since apparently there's no other way around it. Though a workaround, it works quite well. Thanks for the detailed answer by the way, your implementation was quite better than mine :D – emi Jun 7 '18 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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