Crude Example of "Same Page" Login Redirect

Using the Login Redirect plugin gave me "issues" in certain cases - e.g. when a user logs in during checkout (Marketpress). Instead of going to some pre-defined URL (using the redirect plugin) or being dumped into the Dashboard view (default behaviour) I really need the login process to bring me back to the same page.

A quick search threw up this thread from earlier this year:

https://premium.wpmudev.org/forums/topic/last-page-redirect

It gave me an idea how I could get exactly what I wanted, so here's the crude hack I put in place - with it, the value [referrer] can be set in the login redirect config settings, and logins will redirect back to the referring page. Depends on the $_SERVER['HTTP_REFERRER'] value, which is not necessarily recommended as it can be modified on the client side.

Maybe the dev team could incorporate this, and even expand to use other shortcodes (can't think of any off the top of my head though!) Could even be the basis for a paid version of the plugin :slight_smile:

In the file wp-content/plugins/login-redirect/login-redirect.php look for the redirect() method and modify to look like this:

/**
	 * Redirect user on login
	 **/
	function redirect( $redirect_to, $requested_redirect_to, $user ) {
		$interim_login = isset( $_REQUEST['interim-login'] );
		$reauth = empty( $_REQUEST['reauth'] ) ? false : true;

		if( $this->is_plugin_active_for_network( plugin_basename( __FILE__ ) ) )
			$login_redirect_url = get_site_option( 'login_redirect_url' );
		else
			$login_redirect_url = get_option( 'login_redirect_url' );

		// Check for referrer shortcode
		if ("[referrer]" == $login_redirect_url)
		{
			// Shortcode was set in config - check if we were at the main login page
			if (false === strpos($_SERVER['HTTP_REFERER'],"wp-login.php"))
			{
				// Not at main login page so redirect back where we came from
				$login_redirect_url = $_SERVER['HTTP_REFERER'];
			}
			else
			{
				// We were at main login page so redirect back to home page
				$login_redirect_url = "/";
			}
		}

		if ( !is_wp_error( $user ) && !$reauth && !$interim_login && !empty( $login_redirect_url ) ) {
			wp_redirect( $login_redirect_url );
			exit();
		}

		return $redirect_to;
	}

Incidentally, as a quick head start - the added code is found in lines 19 through to 39 in the pasted code snippet above.