skip to Main Content

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 );

2

Answers


  1. Assuming you use the same User Role names, you can use this code:

    function cm_redirect_users_by_role() {
     
        $current_user   = wp_get_current_user();
        $role_name      = $current_user->roles[0];
     // you can change "subscriber" with whatever role you want to redirect
     // change "yoursite.com/" with desired page to redirect users with selected role
        if ( 'subscriber' === $role_name ) {
            wp_redirect( 'http://yoursite.com/dashboard' );
        } // if
     
    } // cm_redirect_users_by_role
    add_action( 'admin_init', 'cm_redirect_users_by_role' );
    
    Login or Signup to reply.
  2. The reason woocommerce_login_redirect filter only work for WooCommerce login is self explanatory as its name. You need to use login_redirect filter to work with WordPress login page. Since both filter arguments is different, I have copied your code and made a few changes as below.

    function redirect_for_wp_login( $redirect, $request, $user ) {
        $current_stats = mfix_show_memberships_details( $user->ID );
    
        if( $current_stats == "active_subscriber" )
            $redirect = "/content-index/";
    
        if( $current_stats == "former_subscriber" )
            $redirect = "/my-account/";
    
        return $redirect;
    }
    add_filter( 'login_redirect', 'redirect_for_wp_login', 10, 3 );
    

    Source: wp-login.php

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search