How to automatically redirect to current post/page when using redirect_login in M2P

I’m using M2P to partially protect content on posts by using this:

Here I place the content that will be available to everyone.
[ms-protect-content id="XX"]
Here I place the content that will ONLY BE VISIBLE to members from the XX membership.
[/ms-protect-content]

And below this, I’m using the M2P shortcode option to redirect the users to the current post after logging in [ms-membership-login redirect_login="https:disappointed:mysite.com/post"], but I see that the shortcode requires the redirect URL to be added manually.

Is there any option to not have to hand-code this last part of setting the URL?

Because we do not insert this code manually into the WordPress editor, instead we insert it into the templates of individual WordPress posts, so we’d like the redirect_login URL to always be the URL the user is on when the post is being opened.

  • James Morris

    Hello Felix

    I hope you are well today.

    Change the shortcode
    [ms-membership-login]
    with

    [ms-membership-login redirect_login="%self-page%"]

    And then use the following code:

    add_filter( 'ms_controller_shortcode_membership_login_atts', function( $atts ) {
    	$atts['redirect_login'] = $atts['redirect_login'] == '%self-page%' ? MS_Model_Pages::current_page() : $atts['redirect_login'];
    
    	return $atts;
    } );

    You can use this code in your child theme's functions.php if the theme is not changed. Otherwise mu-plugin is the best option. To create a mu-plugin, go to wp-content/mu-plugins folder. If there is no mu-plugins folder then, create one. Now, inside the mu-plugins folder create file with name anything.php (make sure file extension is .php). Now start with a <?php tag and then put the above code.

    Hope it helps! Let us know if you have any further questions. We'll be happy to help! :slight_smile:

    Best regards,

    James Morris

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.