I don’t know exactly how this plugin stores the data, but the code to make the login work with them will be something like this.
add_filter( 'authenticate', 'ptstackoverflow_auth_com_cpf_ou_cpnj', 99, 3 );
function ptstackoverflow_auth_com_cpf_ou_cpnj( $user, $username, $password ) {
global $wpdb;
// Se o primeiro parametro não é null, o usuário já está autenticado
if ( $user ) {
return $user;
}
// Passo 1: procurar no banco um usuário que tenha aquele CPF ou CNPJ
// ( que está na variável $username ).
// exemplo, supondo que as meta_keys sejam "cpf" e "cnpj":
$user_row = $wpdb->get_results(
$wpdb->prepare( "SELECT ID FROM {$wpdb->prefix}usermeta
WHERE ( meta_key = 'cpf' AND meta_value = '%s')
OR ( meta_key = 'cnpj' AND meta_value = '%s' )",
$username, $username ) );
// Passo 2: Se encontrou, pega o objeto desse usuário,
// confere a senha
if ( ! empty( $user_row ) ) {
$user = get_user_by( 'ID', $user_row[0]->ID );
// Passo 3: Se a senha confere você retorna o objeto do usuário (WP_User)
if ( wp_check_password( $password, $user->user_pass, $user_row[0]->ID ) ) {
return $user;
}
}
/**
* Se não encontrou ou se a senha não confere, retorne um objeto WP_Error
* com a mensagem que deve aparecer
*/
return new WP_Error( '', 'mensagem de erro', $username );
}
That’s just one example, needs to be modified to work in your case.
updated my answer with the searching code in 2 fields
– Ricardo Moraleida
Thank you Ricardo! Gave it right!
– Flávia Amaral
Perfect! This topic helped me a lot. Thank you all. I was also trying to create the same functionality with login and registration by Ajax on my site, and I ended up adapting my code with the query to the database using the function proposed here. Works well with the plugin Woocommerce Extra Checkout Fields for Brazil, which increases the checkout with the fields Cpf and cnpj https://br.wordpress.org/plugins/woocommerce-extra-checkout-fields-for-brazil/
– Adriano Monecchi