Halloween Sale 15% off Ends in See the plans
PHP

MemberPress: Different Login Destinations per Membership

Sends members to a different page after logging in depending on the membership they hold, for example Gold members to the Gold dashboard.

Don’t have WPIDE yet? Install it on your site

Code

if ( ! class_exists( 'MeprUser' ) ) {
    return;
}

// Membership ID => URL, highest tier first.
$wpide_mepr_login_pages = array(
    456 => home_url( '/gold-dashboard/' ),
    123 => home_url( '/members/' ),
);

$wpide_mepr_login_target = function ( $url, $user ) use ( $wpide_mepr_login_pages ) {
    if ( ! $user instanceof WP_User || user_can( $user, 'edit_posts' ) ) {
        return $url;
    }

    $member = new MeprUser( $user->ID );

    foreach ( $wpide_mepr_login_pages as $membership_id => $page ) {
        if ( $member->is_already_subscribed_to( $membership_id ) ) {
            return $page;
        }
    }

    return $url;
};

add_filter( 'mepr-process-login-redirect-url', $wpide_mepr_login_target, 10, 2 );
add_filter( 'login_redirect', function ( $url, $requested, $user ) use ( $wpide_mepr_login_target ) {
    return $wpide_mepr_login_target( $url, $user );
}, 20, 3 );

Notes

Memberships are checked in the order listed, so put your highest tier first. Works with the MemberPress login form and wp-login.php.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.