Membership 2 Pro - redirect user after registration to original protected content

If an existing member gets to the protected site, a protected content page is being displayed and after login member is redirected back to original content. But if that visitor is not a user/member yet and registers for a user account/membership (following the register link on the Protected Content page), such a user ends up on "Account" page after the registration instead of being redirected back to original page.

The "redirect add-on" won't help here because it can only redirect to a predefined URL while here we want the newly registered user to be redirected back to the original "members only" content after registration.

Is there any code snippet or other solution to achieve that?

  • Dimitris
    • Support Star

    Hello Wendy Lee,

    hope you're doing good today! :slight_smile:

    This is already forwarded to our Second Level Support team, just in case there's any easy workaround. Me or another colleague of mine will keep you posted here as soon as we have some valuable insights. Your patience on this is highly appreciated!

    Take care,
    Dimitris

  • Ivan
    • Developer

    Hi Wendy Lee !

    You could try adding the following snippet in a MU plugin ( more info about MU plugins is here )

    <?php
    function wpmu_membership2_remember_redirect_after_registration( $msg, $type, $obj ) {
    	if ( $msg && !is_user_logged_in() && ( $redirect_to = filter_input( INPUT_GET, 'redirect_to' ) ) ) {
    		setcookie( "membership2_redirect_to", $redirect_to, time() + DAY_IN_SECONDS, '/' );
    	}
    	return $msg;
    }
    add_filter( 'ms_model_settings_get_protection_message', 'wpmu_membership2_remember_redirect_after_registration', 10, 3 );
    
    function wpmu_membership2_save_option( $obj ) {
    	if ( !empty( $_COOKIE['membership2_redirect_to'] ) ) {
    		setcookie( "membership2_redirect", 1, time() + DAY_IN_SECONDS, '/' );
    	}
    }
    add_action( 'ms_model_member_create_new_user', 'wpmu_membership2_save_option' );
    
    function wpmu_membership2_redirect_to( $data ) {
    	if ( !empty( $_COOKIE['membership2_redirect'] ) && !empty( $_COOKIE['membership2_redirect_to'] ) ) {
    		$redirect_to = filter_input( INPUT_COOKIE, 'membership2_redirect_to' );
    		unset( $_COOKIE['membership2_redirect_to'] );
    		unset( $_COOKIE['membership2_redirect'] );
    		setcookie( 'membership2_redirect_to', null, -1, '/' );
    		setcookie( 'membership2_redirect', null, -1, '/' );
    		wp_redirect( $redirect_to );
    		exit;
    	}
    	return $data;
    }
    add_filter( 'ms_controller_shortcode_membership_account_atts', 'wpmu_membership2_redirect_to' );

    I tested it - it should work fine.

    Best,
    Ivan.

  • Wendy Lee
    • Flash Drive

    WOW! Thank you for your team to work on this code. I am embarrassed to say that I'm not sure how I can implement this code? Do I update the PHP file or add the snippet a plugin? Is it the WPMU Dashboard or Membership 2 Pro plugin or another plugin?

    THank you so much!!! I

  • Dimitris
    • Support Star

    Hey there Wendy Lee,

    a little MU (Must Use) plugin, as mentioned above by Ivan should be enough. :slight_smile:
    https://premium.wpmudev.org/manuals/wpmu-manual-2/using-mu-plugins/

    To simplify this for you, you should simply create a new folder in server
    /wp-content/mu-plugins/

    If this is already created, you're fine. Go in this folder and create a new PHP file, for example:
    m2pro-redirection.php
    and add the exact snippet that Ivan shared above. Don't insert any empty lines and/or spaces on top of file, first characters in this file should be <?php

    Warm regards,
    Dimitris

    PS. There's no need for embarrassments here, we always learn new things, don't we? :smiley:

  • Wendy Lee
    • Flash Drive

    Thank you, Dimitri! I was able to add your script. . . I apprciate your additional directions. It was very helpful. I'm not sure if I need to change anything in the settings or update in the M2Pro admin to finish implementation. I tried it and didn't refresh or go back to the original content, after registration. I am currently able to redirect/refresh the content after login, but trying to replicate this for after registration.

    Here is what I did:
    1. Opened this article that is protected.
    https://lifesparkweekly.com/protected-content?redirect_to=https%3A%2F%2Flifesparkweekly.com%3A443%2Ffinding-meaning%2Fstressful-event-quiz
    2. CLick on Register
    3. Submit registration form
    4. Stays on the login page . . . this isn't refreshing/redirecting

    Please let me know what I'm doing wrong! Thanks so much

  • Dimitris
    • Support Star

    Hello there Wendy Lee,

    hope you're doing good today! :slight_smile:

    I just tried to replicate this in your end, so I've tried to register after trying to get to the /quizzes/ page that I see that's protected.
    After doing so, I was redirected to a /member/ page instead of the default "Thank You" page which is set to /registration-complete/.

    Please keep in mind that Ivan's workaround above, changes the link in the Thank You page, so it redirects you back in link of the protected page, where you first came from.

    I can see that you don't use the Redirect Control add-on from Membership 2 Pro, so I wonder how this is happening... :thinking:
    Is it possible that you could perform a conflict test in order to narrow this down?
    Here's a little flow chart image that could assist you on this:
    https://premium.wpmudev.org/wp-content/uploads/2015/09/Support-Process-Support-Process.gif

    Warm regards,
    Dimitris

  • Ivan
    • Developer

    Hi Wendy Lee !

    It seems you discussed in live chat working this snippet with another plugin for registration - so, it won't work. You should replace the ms_model_member_create_new_user hook to similar hook after registration and replace the ms_controller_shortcode_membership_account_atts hook to similar hook before showing page after registration in that plugin.

    Best,
    Ivan.

  • Wendy Lee
    • Flash Drive

    Hi Ivan,

    Would you be able to help me or explain how I can replace the "hook"? I'm not a developer and don't understand how to change hooks. Is there someone that can help me? I tried to Chat with support but they weren't able to understand also.

    Thanks!
    Wendy

  • Ivan
    • Developer

    Hi Wendy Lee !

    If you provide me with additional login credentials, I can look into it more.

    https://premium.wpmudev.org/contact/#i-have-a-different-question
    Please visit our private Contact page and complete the form with the following information:

    Subject: "Attn: Ivan Svyrskyi"

    In the Message box, please provide the following:

    link back to this thread for reference
    link to login page of Social Login plugin
    any other relevant urls

    Admin login:
    Admin username
    Admin password
    Login url

    FTP/SFTP credentials
    host
    username
    password
    (and port if required)

    Note: Don't send any credentials via this forum because it's public forum.

    Best,
    Ivan.

  • Ivan
    • Developer

    Hi Wendy Lee !

    Thank you for providing your access - it works fine. Where can I find the registration page of the Social Login plugin? I found {your_domain}/subscribe-now?step=register page but it uses Membership form. So, how can I replicate the whole process for checking that it doesn't redirect to the protected page after registration? Because it works fine for the Membership form on your site.

    Best,
    Ivan.

  • Wendy Lee
    • Flash Drive

    Thanks Ivan! You are always so helpful.

    The Social Login registration is on the side-bars on all pages (except homepage): https://lifesparkweekly.com/about - See the Connect With area.

    or the Social Login buttons has been added to Membership Pro 2 Registration Pages, so they are working together: https://lifesparkweekly.com/subscribe-now

    I have tested the redirect from the Registration/Subscribe page and it brings me to Membership page: https://lifesparkweekly.com/member

    Since I only have one membership, I'd like to remove or hide the Membership page

    Can I have this go back to the original content page that was protected?

  • Ivan
    • Developer

    Hi Wendy Lee !

    I added the following line to the end of the previous MU plugin
    add_filter( 'ms_controller_shortcode_membership_signup_atts', 'wpmu_membership2_redirect_to' );

    Now it goes back to the original content page that was protected but anyway this content is protected for new users because new users have no any membership. You should set Standart Membership to assign to each new member for it.

    Best,
    Ivan.

  • Wendy Lee
    • Flash Drive

    Thanks, Ivan! I'm testing now and for some reason...all of the new Memberships that I'm creating are not showing up in the ALL MEMBERS page in the Membership 2 Pro Admin. See the
    [image pos="0"]

    I'm unable to test this new functionality of redirecting to the original content page.
    I am coming from this page: https://lifesparkweekly.com/finding-meaning/stressful-event-quiz

    I keep getting redirected to this page, which just shows the protected message: https://lifesparkweekly.com/protected-content?redirect_to=https%3A%2F%2Flifesparkweekly.com%3A443%2Ffinding-meaning%2Fstressful-event-quiz

    Am I doing something wrong? Thanks so much!

  • Ivan
    • Developer

    Hi Wendy Lee !

    Happy New Year!

    It seems it happened because you activated Simulation mode. I created the test Membership and it shows there

    I keep getting redirected to this page, which just shows the protected message: https://lifesparkweekly.com/protected-content?redirect_to=https%3A%2F%2Flifesparkweekly.com%3A443%2Ffinding-meaning%2Fstressful-event-quiz

    It happens because new user doesn't have access to this page. This page available for the Lifespark Exclusive membership but he doesn't have it.

    Best,
    Ivan.

  • Wendy Lee
    • Flash Drive

    Thank you, Ivan. I see the Test Membership that you set up. I keep testing and trying to add New Members with new email addresses and it's not showing up in my Membership list.
    You mentioned in your last thread that it's because I'm in Simulation mode, but I created the account in another browser via Incognito mode
    Am i Doing something wrong?

  • Wendy Lee
    • Flash Drive

    In addition to the note above from yesterday, about not able to get members to Register and appear in my Membership List (it's only showing up in my Users and not attached to a Membership). . . I am also noticing that my page won't redirect to the protected content because there are currently no member emails that are activated. Can you help me with this?

  • Ivan
    • Developer

    Hi Wendy Lee !

    Sorry for confusing. Regarding the Simulation mode - it was my answer to it -

    all of the new Memberships that I'm creating are not showing up in the ALL MEMBERS page in the Membership 2 Pro Admin.

    A user doesn't have any membership because he should select it before or after registration. Only membership with Default Membership type will assign automatically after registration.

    So, you can create a new membership with this type, unprotect need pages for this membership and check the redirect after registration again.

    I am also noticing that my page won't redirect to the protected content because there are currently no member emails that are activated.

    Could you explain it more, please?

    Best,
    Ivan.

  • Wendy Lee
    • Flash Drive

    Hi Ivan, I only have one membership at this time. Can they default to that membership? I'm not seeing any New Members when I test new registration. See here:

    I see new email address under Wordpress Dashboard, USERS page, but no membership tied.

    Most importantly, I need to test the RE-DIRECT from New Registration/New User. As of last time we talked, it still is not working for me. You had said that it's working, but I'm not able to replicate it. THANK YOU SO MUCH, IVAN

  • Ivan
    • Developer

    Hi Wendy Lee !

    You had said that it's working, but I'm not able to replicate it.

    I tested the redirect and it works but as a new user doesn't have any membership - it redirects to the Protected page because a new user doesn't have access to protected content.
    You can test it in the following way - create a new membership with the Default Membership type, create a new protected page for this membership and test it again. Did you do it?

    Can they default to that membership?

    Unfortunately no, you can't change type of this membership to Default. You need to create a new one.

    I see new email address under Wordpress Dashboard, USERS page, but no membership tied.

    You're right. Because the Lifespark Exclusive membership has another type of membership, not the Default Membership type.

    Best,
    Ivan.

  • Wendy Lee
    • Flash Drive

    Patrick helped me fix the New Member issue, so no problem with that. Now that I can test the New Register Redirect...I end up at the Thank you page.

    Step 1. Click on Protected Content URL: https://lifesparkweekly.com/thinking-flexibly/coping-flexibility-quiz
    Step 2. Click Subscribe on protected page
    Step 3. Click Subscribe again on subscribe/member page
    Step 4. Complete & Submit form
    This brings me to my thank you page, rather than the original protected content pg. Here is the URL; https://lifesparkweekly.com/registration-complete?ms_relationship_id=7432

    Thank you!

  • Wendy Lee
    • Flash Drive

    Hi Ivan, we had to make some changes with your Membership Pro 2 support team and I wanted to make sure the Redirect code is still in place. Would you be able to see if the one line at the end of the MU plugin on my site still works? I tested the REDIRECT and it doesn't seem to be working. THANK YOU!

  • Dimitris
    • Support Star

    Hello there Wendy Lee,

    hope you're doing good today and don't mind me chipping in! :slight_smile:

    I've just tried to replicate this, visiting the /making-meaning-loss-5-days/ page. After registering my new testing user from the protected page that I first got, I was redirected back to the initial protected page, so this seems to work well!

    Could you please try to make another little test like the above and let me know how that goes?

    Warm regards,
    Dimitris

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.