'Redirect at first login in Wordpress/Woocommerce
Good morning,
I can't do a redirect on the first wordpress login for clients, I created a "has_not_logged_in_yet" field on the database, but when I go to do an update, the update_user_meta function with this "woocommerce_login_redirect" hook doesn't work. If I use "init" as a hook, update_user_meta works but the redirect doesn't work. I am attaching the code, you can help me? Thank you!
function after_user_register( $user_id ){
add_user_meta($user_id, "has_not_logged_in_yet", "true", true);
}
add_action( 'user_register', 'after_user_register', 10, 1 );
function wc_custom_user_redirect( $redirect, $user ) {
$role = $user->roles[0];
$id= $user->ID;
$dashboard = admin_url();
$catalogo = get_permalink( wc_get_page_id( 'shop' ) );
$changePWeAddress = get_permalink( get_page_by_title( 'Conferma Indirizzo' ) );
if ( $role == 'customer' ) {
$user_meta=get_user_meta($id);
if($user_meta['has_not_logged_in_yet'][0] == "true"){
update_user_meta($user_id, "has_not_logged_in_yet","false");
$redirect = $changePWeAddress;
}
else
$redirect = $catalogo;
}
return $redirect
}
add_filter( 'woocommerce_login_redirect', 'wc_custom_user_redirect', 10, 2 );
Solution 1:[1]
There are several syntax errors. I have revised your code. Also, you are passing $user_id variable to update_user_meta
Replace
update_user_meta( $user_id, "has_not_logged_in_yet","false" );
With this
update_user_meta( $id, "has_not_logged_in_yet","false" );
Complete Code
function after_user_register( $user_id ){
add_user_meta( $user_id, "has_not_logged_in_yet", "true", true );
}
add_action( 'user_register', 'after_user_register', 10, 1 );
function wc_custom_user_redirect( $redirect, $user ) {
$role = $user->roles[0];
$id = $user->ID;
$dashboard = admin_url();
$catalogo = get_permalink( wc_get_page_id( 'shop' ) );
$changePWeAddress = get_permalink( get_page_by_title( 'Conferma Indirizzo' ) );
if ( $role == 'customer' ) {
$user_meta = get_user_meta($id);
if( $user_meta['has_not_logged_in_yet'][0] == "true" ){
update_user_meta( $id, "has_not_logged_in_yet","false" );
$redirect = $changePWeAddress;
} else{
$redirect = $catalogo;
}
return $redirect;
}
}
add_filter( 'woocommerce_login_redirect', 'wc_custom_user_redirect', 10, 2 );
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bhautik |
