A member on your site is reading a post with protected content. This can be either fully protected so they see a prompt to login or partially protected so they see some teaser content with a prompt. In order to see the full content, they must login. After clicking the login link, they end up on the Login page. By default, after logging in, that member is immediately directed to the "Home Page" you have defined in the Member Homepage Settings. The following article outlines how you can change this behavior and instead direct the member back to the post they were just reading.


Option 1: Use a Hidden HTML Field


You can use a similar approach to the example that a User on StackExchange posted as their solution. The following code will grab the URI the user was on before they attempted to login, no matter the string parameters, then redirect them back to that same page after a successful login. Simply include the highlighted code below on your login page in-between the [MM_Form type='login'][/MM_Form] like shown in the example:


[MM_Form type='login']

...

<input type="hidden" name="redirect_to" value="?php echo $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];?">

...

[/MM_Form]



PLEASE NOTE: Though MemberMouse is sharing this 3rd party approach, we can not support this solution if you should run into any issues trying to use this script. This is a solution provided to you purely out of one of our support team's personal experience and any unexpected situations you may encounter trying to use this script will be outside of our support policy.



Option 2: Use a Custom PHP Script


If the above solution does not work on your WordPress installation, a MemberMouse user named Thiago has shared his resolution, and has given us permission to share it with other members. You will need to replace the Page ID values in the script with the IDs of the appropriate pages on your site. Then, using your preferred approach to adding PHP to your site, insert the modified code. 


/*  Redirect back to previously viewed page after login. Based on script by Thiago Santos. 

	When the customer is viewing a page that is not Login, Logout, Member Home, etc,
	we will set a cookie that contains the ID number of the page.

	By default, when the member logs in, they are redirected to the Member Home Core Page.
	When this happens, we check to see if the cookie is set, redirect to the page ID 
	in the cookie, and clear the cookie so that redirect only happens once, at login.
	
	Note that if you change the Member Homepage Settings, you will need to specify a 
	different page, typically the site homepage. If you have a specific homepage for 
	certain membership levels, you will need to test for the ID of each of these pages 
	when deciding whether to redirect back to the page identified by the cookie.  */


function mm_redirect_on_login() {
	$mmPageIdCookie = 'mmPageIdCookie';
	
	/*  BEGIN CUSTOMIZATION
	Please see these 3rd-Party Instructions on finding the ID of a WordPress Page:

	 https://www.ostraining.com/blog/wordpress/how-to-find-the-page-id-in-wordpress/

	For each variable below, replace the value with the ID of the specified page  */

	// Login Core Page ID
	$loginPID = 10;
	// Logout Core Page ID
	$logoutPID = 11;
	// Error Core Page ID
	$errorPID = 12;
	// Forgot Password Core Page ID
	$forgotPID = 13;
	// Reset Password Core Page ID
	$resetPID = 14;
	// Member Home Page ID
	$mbrHomePID = 15;

	/*  END CUSTOMIZATION  */

	if ( !is_user_logged_in() 
	  && !is_page($loginPID) 
	  && !is_page($logoutPID) 
	  && !is_page($errorPID) 
	  && !is_page($forgotPID) 
	  && !is_page($resetPID) 
	  && is_single() ) {
		$currentPID = get_the_id();
		setcookie($mmPageIdCookie, $currentPID, time() + 3600, '/');
	}

	if ( is_user_logged_in() 
	  && is_page($mbrHomePID) 
	  && isset($_COOKIE[$mmPageIdCookie]) ) {
		$redirectURL = get_permalink($_COOKIE[$mmPageIdCookie]);
		setcookie($mmPageIdCookie, null, time() - 3600, '/');
		wp_safe_redirect($redirectURL, 307);
		exit();
	}

}

add_action('template_redirect', 'mm_redirect_on_login');

function mm_set_redirect_cookie_nobypass( $data ) {

	if ( !is_user_logged_in() && is_single() ) {
		$mmPageIdCookie = 'mmPageIdCookie';
		$currentPID = get_the_id();
		setcookie($mmPageIdCookie, $currentPID, time() + 3600, '/');
	}
	return false;
}

add_filter('mm_bypass_content_protection', 'mm_set_redirect_cookie_nobypass');


PLEASE NOTE: Though MemberMouse is sharing this 3rd party approach, this is not a solution that we can support. This is a solution provided to you purely out of one of our customer's personal experience and we can not support any issues you may encounter trying to use this script if it does not function as expected on your site.