Wordpress change Autentication From username to email - php

How or where I must edit wordpress to make user autentication with email and not with username (as it is default)

This should work, add this to the functions.php file:
// remove the default filter
remove_filter( 'authenticate', 'wp_authenticate_username_password', 20, 3 );
// add custom filter
add_filter( 'authenticate', 'fb_authenticate_username_password', 20, 3 );
function fb_authenticate_username_password( $user, $username, $password ) {
// If an email address is entered in the username box,
// then look up the matching username and authenticate as per normal, using that.
if ( ! empty( $username ) )
$user = get_user_by( 'email', $username );
if ( isset( $user->user_login, $user ) )
$username = $user->user_login;
// using the username found when looking up via email
return wp_authenticate_username_password( NULL, $username, $password );
}
(The above was found here and I tested it and worked for me)
EDIT: This plugin works as well if you don't want to modify the functions file.

Related

How To Set global current_user on Wordpress Using PHP?

I am using the following code to login a user.
$UserName and $Password is coming in via a Form. The following if statement is showing TRUE, so my user is getting logged in.
$user = get_user_by( 'login', $UserName );
if ( $user && wp_check_password( $Password, $user->data->user_pass, $user->ID) )
Now as long as the User is on the website I want his $UserName to be known by all the PHP programs he invokes.
Whenever I log someone in using the wp-admin screen the $UserName is brought in to all PHP programs by executing the following code:
global $current_user;
if ( isset($current_user) ) {
$UserName=$current_user->user_login;
}
My question is.... using the login at the top of this post how do I save the $Username in such a way that it is brought into other PHP programs using the above isset($current_user) if statement?
I tried the following code but it does not bring the $current_user field into other PHP programs by executing the IF (isset($current_user) statement. The $user->user_login field contents is equal to $UserName.:
$user_id = $UserName;
$user = get_user_by( 'id', $user_id );
$current_user= new WP_User( $user_id , $user->user_login );
wp_set_auth_cookie( $user_id );
do_action( 'wp_login', $user->user_login );
global $current_user;
What am I missing? Any assistance will be greatly appreciated. Thank you

Update user meta data after Woocommerce new user registration

Im trying to add a custom field to WP_USERMETA table after Woocommerce registration
add_filter('woocommerce_new_customer_data', 'wc_assign_custom_role', 10, 1);
function wc_assign_custom_role($args) {
update_user_meta($user_id, 'user_pass2', password_hash($_POST['password'], PASSWORD_DEFAULT));
return $args;
}
as you see Im trying to capture password before hashing and save it in a different hash format in that table
but It doesnt add anything to the table
I tested the same line inside wordpress registration hook user_register and it worked but only for wordpress registration not woocommerce
UPDATE
add_filter('woocommerce_new_customer_data', 'wc_assign_custom_role', 10, 1);
function wc_assign_custom_role($args) {
global $current_user;
update_user_meta($current_user->$user_id, 'user_pass2', password_hash($_POST['password'], PASSWORD_DEFAULT));
return $args;
}
still doesnt work
UPDATE II
function action_woocommerce_created_customer( $customer_id, $new_customer_data, $password_generated ) {
update_user_meta($customer_id, 'user_pass2', password_hash($_POST['password'], PASSWORD_DEFAULT));
};
add_action( 'woocommerce_created_customer', 'action_woocommerce_created_customer', 10, 3 );
this one create meta data but it seems it uses different $_POST['password'] rather than the password I entered, so hash something else rather than password
Any thoughts??
When looking at the source code where woocommerce_created_customer action hook is located, the password can be found as $new_customer_data['user_pass'] (see at the end of the answer).
So your code should be:
add_action( 'woocommerce_created_customer', 'action_woocommerce_created_customer', 10, 3 );
function action_woocommerce_created_customer( $customer_id, $new_customer_data, $password_generated ) {
update_user_meta($customer_id, 'user_pass2', password_hash($new_customer_data['user_pass'], PASSWORD_DEFAULT));
}
Code goes in function.php file of your active child theme (or active theme). It should work.
Here is the related source code involved from wc_create_new_customer() function:
$new_customer_data = apply_filters( 'woocommerce_new_customer_data', array(
'user_login' => $username,
'user_pass' => $password,
'user_email' => $email,
'role' => 'customer',
) );
and $_POST['account_password'] is not required as it's already stored in $password variable.
Found the solution, We should use $_POST['account_password'] instead of $_POST['password']
function action_woocommerce_created_customer( $customer_id, $new_customer_data, $password_generated ) {
update_user_meta($customer_id, 'user_pass2', password_hash($_POST['account_password'], PASSWORD_DEFAULT));
};
add_action( 'woocommerce_created_customer', 'action_woocommerce_created_customer', 10, 3 );

Create user on order status completed action hook in WooCommerce 3+

I am trying to create a new user within the woocommerce_order_status_completed hook.
add_action( 'woocommerce_order_status_completed', 'custom_woocommerce_order_status_completed', 10, 1 );
function custom_woocommerce_order_status_completed( $order_id )
{
$password = wp_generate_password( 16, true );
$user_id = wp_create_user( 'usertest#gmail.com', $password, 'usertest#gmail.com' );
}
$user_id returns an actual ID. It appears to create the user but when i look at the backend the user is not there. I even check the database for the user id and it's not there.
If i call the same function on the action woocommerce_after_register_post_type it creates the user.
Any idea what could be causing this issue?
The following code uses Woocommerce wc_create_new_customer() dedicated function instead:
add_action( 'woocommerce_order_status_completed', 'action_on_order_status_completed', 20, 2 );
function action_on_order_status_completed( $order_id, $order ){
$password = wp_generate_password( 16, true );
$user_name = $user_email = 'usertest#gmail.com';
// $user_name = $user_email = $order->get_billing_email();
$customer_id = wc_create_new_customer( $user_email, $user_name, $password );
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
It looks like your function name does not match the one call within your hook:
custom_woocommerce_order_status_completed

Set full email address as username when registering through WooCommerce

WooCommerce under Settings/Account has an option named
Automatically generate username from customer email
but the username it generates is not the full email address.
my.email#example.com
becomes myemail as a username.
What method would I need to hook into to override the user generation so that the
full email is set as the username?
You can use something like this in your functions.php. The woocommerce_new_customer_username filter could be used to change the generated username. If you just return the customer email as the username, it will be used instead of the generated one (in your case the name before #).
function my_new_customer_username( $username, $email, $new_user_args, $suffix ) {
return $email;
}
add_filter( 'woocommerce_new_customer_username', 'my_new_customer_username', 10, 4 );
Simply use the hook pre_user_login
add_filter( 'pre_user_login' , 'wpso_same_user_email' );
function wpso_same_user_email( $user_login ) {
if( isset($_POST['billing_email'] ) ) {
$user_login = $_POST['billing_email'];
}
if( isset($_POST['email'] ) ) {
$user_login = $_POST['email'];
}
return $user_login;
}
Sorry for reviving an old thread, but it seems this is the best option (goes in your child functions.php or custom plugin)
add_filter( 'woocommerce_new_customer_data', function( $data ) {
$data['user_login'] = $data['user_email'];
return $data;
} );
here are 2 source references: 1 and 2

User login by email in wordpress is not working

I have been trying to enable the user login by email. I found at wordpress codex and some few blogs the way to write the following code in functions.php
add_action( 'wp_authenticate', 'wp_authenticate_by_email' );
function wp_authenticate_by_email( $username ) {
$user = get_user_by( 'email', $username );
if ( empty( $user ) ) {
return;
}
return $user->user_login;
}
But it does not work for me. I found it is remained as earlier.
I had a similar issue but got this to work:
function my_authenticate_by_email( $user, $username, $password ) {
//try to get user by email
$user = get_user_by( 'email', $username );
//validate we got a user.
if ($user && !is_wp_error($user))
{
//use normal auth
return wp_authenticate_username_password(null, $user->user_login, $password);
}
//continue to next filter, like normal.
return null;
}
add_filter( 'authenticate', 'my_authenticate_by_email', 0, 3);

Categories