How to display login form and stay on the same protected post/page after login?

For this example, let's say a user is on a protected post or page and currently is logged out. They must log in through the login link in that post. When this happens, the user is redirected to the page that it is set in the "URL to direct member to after login:" field inside MemberPress → Settings → Account tab.

What if you want to display a login form on the protected post/page and allow users to stay in the same place without being redirected to another page?

Displaying login form on post/page

There are three ways for displaying login form on post/page so if you don't see login form as an unauthorized user make sure you check the settings below in this order. 

1. Settings Page

Head to the MemberPress → Settings → Pages tab and enable "Show a login form on pages containing unauthorized content" option.

If you want to have more control over the position of the login form you can also disable "Show a login form on pages containing unauthorized content" option and click "Default Unauthorized Message" link. This will reveal content editor so you can add [mepr-login-form] shortcode where you want

2. Rule

The next place where you can change login form settings is the Rule page.

Open the rule that protects page/post under MemberPress → Rules page and leave it as "Default" if you want to keep settings from the global settings you have set above.

If you disabled login form in global settings then you can select "Show" option to display login form only on post/page protected with this rule.

3. Post or Page

Similar options can be set in each post/page separately so if you left default option in MemberPress' Settings you can leave it as "Default" in post/page to display the login form. 

If you disabled login form globally in MemberPress' Settings and left it as hidden in rules but you want to display it on a specific page then select "Show" option.

Displaying login form on part of page protected by shortcode

If you are using [mepr-active] shortcode to protect some element on the post/page you can use a special parameter unauth that is responsible for displaying login form when a user is unauthorized to access the content.

Here is the shortcode to display both message and login form: [mepr-active rule="123" unauth="both"] and here is the shortcode that displays login form: [mepr-active rule="123" unauth="login"]

If you want to know more about shortcodes, please review this article.

Displaying login form on Unauthorized Redirect

When you have a page set up as an unauthorized page in MemberPress → Settings → Pages tab like you can see below.

then you can add a login form shortcode to that page as well.

This way if not logged in users open a protected page they will be redirected to /unauthorized page with login form. As soon as they submit login form they will be redirected back to the post/page where they came from. 

Do NOT check the "Use MemberPress Login Redirect URL" option in this shortcode if you want to redirect user to the post/page they came from.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.