Redirecting non-members from a restricted page

The default is that non-members (or logged-out members) trying to access restricted content are redirected to the MOP (membership options page). But, what if I want them to be redirected to the wp-login.php page? The closest thing I found is this forum post:

But, that solves the problem of having the now-logged in user go to where they were wanting to go, and not to the Login Welcome Page, which is the default (I want to maintain this default behavior). Do I just need to modify that inscrutable code to work with default login behavior?

Side question about the MOP: Must the Paypal button only be located on the MOP? I ask because maybe I can just make wp-login.php the MOP, and then create some other page with the Paypal button which would not be called the MOP? Or does S2 handshaking with Paypal only function when the Paypal button is on the officially designated MOP?

Thank-you for your help!!

You can’t have selective redirection for people who are not logged in. It is only after they log in that you can differentiate them and provide differentiated UX. Before they log in we know nothing of significance to base a redirection on.

I think what you’re saying is that it’s not possible to change the default behavior of S2Member, which is to redirect anyone who is not logged in to the MOP.
And my other question, does the PayPal button need to be on the MOP in order for the handshaking to work? If the button works properly anywhere, then I can make the login page the MOP and solve my problem.

It is possible to bypass the MOP completely. Take a look here at how to redirect to the login page instead: Disable redirect to membership sign-up

Yes, that’s the link that I referred to above in my original post of this thread. But, that seems to bypass the default behavior of directing the user to the Login Welcome Page, and instead directs the newly logged in user to the place where he was trying to go before he was logged in. I would use this code if I better understood it, so that I could adjust it to allow for default S2 behavior of directing to the Login Welcome Page. I just thought there might be a simpler solution, like making wp-login.php the MOP, and putting the PayPal button on a different page not designated as the MOP.

Hi Tim,
I did try to modify your code, and I think it’s working, but maybe you can confirm. I commented out the line:
$redirect = add_query_arg( ‘redirect_to’, urlencode( $URI ), $redirect );

And, I commented out the whole second function s2_redirect as well as the add_filter call.

Please let me know that’s correct. Thank-you!

You do not need to put your PayPal buttons on your MOP. Best way to validate that is to put the button on another page and test it.

Having said that…you never said why you want to redirect people to the login page.

In general when asking for advice it is better to state your problem rather than presume a solution and then ask questions about the solution. I and others on communities like this know multiple solutions to any problem. If we knew what problem you are trying to solve it would make it easier for all of us to get you a solution with the least amount of energy.

As it stands you are resorting to a technical code solution yet you do not come across as a PHP coder. So if it breaks because of a PHP upgrade you won’t know how to fix it.

If I might ask…who do you want to redirect to the login page and why?

People coming to your site are either:

  • unregistered
  • registered / not logged-in
  • registered / logged-in with access to unprotected content,
  • registered / logged-in with access to some unprotected and some protected content,
  • registered / logged-in with access to protected content (in the all-or-nothing case).

Although you don’t need to put your PayPal button on the MOP the [s2MOP] shortcode is only available on the MOP page which is a useful shortcode for non-coders to customise the user’s post-login experience. See https://s2member.com/kb-article/s2mop-shortcode/

So your first idea should work…just not sure why you would want to do it that way and give up using the [s2MOP] shortcode. Combined with the [s2if] shortcode you can do quite a lot of non-coding customisation (see https://s2member.com/kb-article/s2if-simple-shortcode-conditionals/).

Okay, thank-you for all that, Tim!
Yes, I see that the s2mop thing might work, because then when the non-member or logged-out member is redirected to the MOP, an additional message can appear, saying that if they’re already a member, then just log in, no need to click Paypal button, and if they aren’t a member, here’s the page to become a member. That’s what I had been afraid of, that 99% of the people trying to click on restricted content are simply members who simply aren’t logged in, and so I wanted them redirected to the login page.
Also good to know that Paypal button can go anywhere, though of course as you say I should test this out (if I go that route).
For now, I’m going with that code snippet, with my crude modification. I know a little php, enough to be dangerous!
Thank-you again for your thorough reply.

No worries.

You know you can also just add a sidebar to your site and add in a [s2Member-Login /] login form shortcode wrappered with a [s2if] shortcode to selectively display the form.

Saves you having to use custom code and eliminates a redirect.

See https://s2member.com/kb-article/s2member-login-shortcode-documentation/

1 Like

What you have done will indeed cause the user to be redirected to the login page. But that’s all it will do.

The purpose of the rest of code is to ensure that, if someone is trying to access a specific URL while logged out, then not only will s/he get redirected to the login page initially, but s/he’ll also then be redirected to the intended URL after logging in.

1 Like

Great work on putting that code together, and thank-you for confirming my crude hack!

1 Like