[MEMBERSHIP] redirect after login onto the protected page the user landed on

So basically when a user is taken to a URL that is protected from other people viewing it, when they land on this they'll be prompted to login.

Once they do it automatically redirects them to the "Account" Page I set within the settings.
My goal was to see if that original page they were trying to access (the one that was "protected") could be the page that they are redirected too, not the account page.

Any assistance would be appreciated!

  • Predrag Dubajic
    • Support

    Hi Thomas,

    We have pinged our devs to check this out further, just to be clear, you're referring to users that are not yet registered and that after they finish with registration you want them to redirect to page they were on, not for already registered users that only need to login, right?

    Best regards,

  • Thomas
    • WPMU DEV Initiate

    Hi Predrag,

    No that's not what I'm referring to - sorry for the confusion on this issue. The way I've set this up is that NO ONE can register on this site public-side. Someone on the back-end always would need to create new members. Which is how I want it to be - so please ignore anything to do with registration for this issue.

    This is for members who HAVE a login, but just aren't logged into the site when they attempt to view a protected content page. Once they login from that page the user is automatically taken to the page I've set as the "Account" page - and not the URL they were originally trying to view (which was seen as "protected content".)

    As an example:

    If I'm a member and someone else who is a member shared a page URL with me to view - and I clicked onto it. Instead of it taking me to that actual page URL it would take me to the "protected content" page. Which is how it should work if you aren't logged in.

    While I'm on that "protected content" page there is a place to login. I go ahead and login to my account. Now from here instead of being redirected to that original URL I was trying to access (the page that is under the "protected content") I'm taken to the "Account" Page.

    Therefore I lose the URL I was originally trying to access. With the way I've set the membership pro 2 plugin up, I've created custom post types for members only to view (I've already accomplished this). So if someone were to share a post that is protected, they would lose that post URL and need to dig further to find it.

    So the goal I'm trying to achieve is: for members who aren't logged in yet and land on a protected "content page" - once they log in I want them to be redirected to the page they 1st tried to access. Not have them redirected to the page I've set at the "Account" page.

    Does that further help? If you need further clarification please let me know. Thank you for the assistance.

  • Thomas
    • WPMU DEV Initiate

    Yes the protected page is set up via the admin and also with additions to the functions.php file. There is the chance I accidentally set something customize to make that happen with the admin settings. If you could help me pin-point that issue (whether it was on my end or on the plugins).

    As for an example this is a post example: (please add this after the domain)

    You should now have access, thanks for the help on this.

  • Panos
    • SLS

    Hi Thomas ,

    That seems to be a custom post type, that's why you don't see any protection options when editing that CPT. I see you are trying to achieve the protection with a custom snippet in functions.php filtering pre_get_posts. Membership has an add-on which you can activate to protect single custom post instead of using a custom snippet. It's called Individual Custom Posts which you can find under: admin > Membership 2 > Add ons

    After activating that, you will see a new Tab with title Custom Post Type Items under admin > Membership 2 > Protection Rules. From there you can find your posts and set the memberships that are allowed to access them. Following this you would not need to use the custom snippet in functions.php.

    In case you prefer to still use the snippet, you will need to add the redirect_to query arg on the url of the protection page. Here is an example that worked for me:

    add_filter( 'pre_get_posts', 'redirect_from_events' );
    function redirect_from_events( $query ) {
    	if ( is_admin() || ! $query->is_main_query() || 'YOUR_CPT' != $query->get('post_type') ){
    	$redirect_url = false;
    	if ( ! is_user_logged_in() && isset( $query->query['name'] ) && isset( $query->query['post_type'] ) ) {
    		global $wpdb;
        	$page_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s", $query->query['name'], $query->query['post_type'] ) );
    		if ( $page_id ) {
    			$redirect_url = get_permalink( (int) $page_id );
    	// Look for a page with a slug of "logged-in-users-only".
    	$target_page = get_posts( array(
    		'post_type' => 'page',
    		'name' => 'protected-content'
    	) );
    	// Use the target page URL if found, else use the home page URL.
    	if ( empty( $target_page ) ) {
    		$url = get_home_url();
    	} else {
    		$target_page = current( $target_page );
    		$url = get_permalink( $target_page->ID );
    		if ( $redirect_url )  {
    			$url = add_query_arg(
    	// Redirect!
    	wp_safe_redirect( $url );

    In case you decide to test this snippet, make sure you replace YOUR_CPT with your actual custom post type.

    Kind regards!

  • Thomas
    • WPMU DEV Initiate

    The original issue I had with using the add-on "Individual Custom Posts" was that you would manually need to select which posts were protected from the Protection Rules area. (With this add-on there isn't a way to protect the custom post type as a whole - both the archive and the single posts it produces.). Which is why I went with the code snippet approach. Unless I'm mistaken on the above - could you let me know if that's correct?

    But the above snippet you provided worked great for me! Thank you for the assistance on this!

  • Thomas
    • WPMU DEV Initiate

    Correct - on my site it does not work that way. When the "Individual Custom Posts" add-on is active I only see the following under Membership 2 < Protection Rules < Individual Post Type Items.

    This shows a list of individual post type items that I can manually protect or not-protect. My goal ideally would be to protect the entire custom post type - both past and future individual posts with this method.

    If that's not possible the code snippet will do - but I've given you support access to the site if you'd like to take a look for me. Thanks!

  • Predrag Dubajic
    • Support

    Hi Thomas,

    Sorry if my previous message was unclear, I was actually referring to "Individual Custom Posts" add-on being disabled.
    Once this is disabled M2 will use the default post protection option which is blocking the entire post type.
    When "Individual Custom Posts" add-on is disabled you can go to Membership 2 > Protection Rules and you should see Custom Post Types tab there.
    In that tab you will see list of your registered Custom Post Types and protecting it will protect all posts in that CPT, existing and newly created ones:

    Hope this clears it up more :slight_smile:

    Best regards,

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.