I would like to redirect users when they login, based on their WooCommerce Membership level. I found this functions.php script (see below) but can't get it to work for the wp-login, it only works for the WooCommerce login. Is there an easy way to adjust it?
function mfix_show_memberships_details ($curr_user_id) {
if ( is_admin() ) {
return;
}
$customer_memberships = wc_memberships_get_user_memberships( $curr_user_id );
$former_subscriber = [];
$active_subscriber = [];
foreach ( $customer_memberships as $customer_membership ) {
$membership_plans = $customer_membership -> get_plan() -> get_name();
$membership_plans_array[] = $customer_membership->get_plan()->get_name();
$membership_plans_status = $customer_membership->get_status();
$membership_plans_status_array[] = $customer_membership->get_status();
if (($membership_plans == "Full Access Subscription") && ($membership_plans_status == "cancelled")) {$former_subscriber[] = "former_subscriber";}
if (($membership_plans == "Full Access Subscription") && ($membership_plans_status == "expired")) {$former_subscriber[] = "former_subscriber";}
if (($membership_plans == "Full Access Subscription") && ($membership_plans_status == "active")) {$active_subscriber[] = "active_subscriber";}
}
if(!empty($active_subscriber)){
$current_stats = "active_subscriber";
return $current_stats;
exit();
}
else if (!empty($former_subscriber)){
$current_stats = "former_subscriber";
return $current_stats;
exit();
}
else {
$current_stats = "potential_member";
return $current_stats;
exit();
}
}
function mfix_custom_user_redirect( $redirect, $user ) {
$username = $user->user_login;
$curr_user = get_user_by('login',$username);
$curr_user_id = $curr_user->ID;
$current_stats = mfix_show_memberships_details ($curr_user_id);
if( $current_stats == "active_subscriber" ) {
// $redirect = "/start-here/";
$redirect = "/content-index/";
}
if( $current_stats == "former_subscriber" ) {
$redirect = "/my-account/";
}
return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'mfix_custom_user_redirect', 10, 2 );
My Answer:
> One way to achieve this is by using the `wp_login` hook in WordPress. You can add a function to this hook that checks the user's WooCommerce Membership level and then redirects them accordingly. Here's an example of how you can modify the existing function to work for the wp-login:
php
function mfix_redirect_on_login($user_login, $user) {
$curr_user_id = $user->ID;
$customer_memberships = wc_memberships_get_user_memberships( $curr_user_id );
foreach ( $customer_memberships as $customer_membership ) {
// Check the user's membership level and redirect accordingly
if ( $customer_membership->get_plan_id() == 'your_membership_level_id_here' ) {
wp_redirect( 'your_redirect_url_here' );
exit;
}
}
}
add_action('wp_login', 'mfix_redirect_on_login', 10, 2);
Replace `'your_membership_level_id_here'` with the ID of the WooCommerce Membership level you want to check for, and `'your_redirect_url_here'` with the URL you want to redirect the user to if they have that membership level.
Make sure to add this code to your theme's `functions.php` file or a custom plugin. This should now redirect users based on their WooCommerce Membership level when they log in through the wp-login page.
Rate this post
5 of 5 based on 8850 votesComments