Redirect users based on their logged in / logged out status

I would like to replace the text of my “LOGIN” navigation menu, with the text “MEMBERS AREA” to achieve the following:

  • If user clicks on MEMBERS AREA nav - and user is already logged in – they are redirected to a protected page
  • If user clicks on MEMBERS AREA nav - and user is not logged in – they are redirected to wp-login.php (not the MOP page like we have it set up at the moment for non-logged in users who try to go to protected pages)

Additionally, I would like to redirect any logged in users away from the MOP page to a protected page.

Is this possible?

Thanks!

For the first question, you can just use a standard redirect. Put something like this in your child theme’s functions.php file:

function member_redirects() {
	if ( is_user_logged_in() && is_page( 'members' ) ) {
		wp_redirect( home_url( '/protected-page/' ) );
		exit;
	}
}
add_action( 'template_redirect', 'member_redirects' );

For the rest, it sounds like you just need to add conditionals to the MOP to show different content to logged-in and logged-out users, including maybe a link to the login page.

Thanks @KTS915. I’ll add this to functions.php

For the rest, it sounds like you just need to add conditionals to the MOP to show different content to logged-in and logged-out users, including maybe a link to the login page.

Can you advise how I add conditionals such as this within S2MemberPro admin panel? Or is it a code function?

Look at API/Scripting -> Simple/Shortcode Conditionals

Thanks @KTS915 Please forgive my limited code knowledge, but regarding the .php function for member redirects … am I correct in replacing (‘members’) with say ‘wp-login.php’ if the login page is /wp-login.php or should this be a page slug such as ‘/members/’?

I’ve replaced the ‘protected-page’ with ‘/name-of-page/’

 function member_redirects() {
	if ( is_user_logged_in() && is_page( 'members' ) ) {
		wp_redirect( home_url( '/protected-page/' ) );
		exit;
	}
}
add_action( 'template_redirect', 'member_redirects' );

That wouldn’t make much sense because the user would already be logged in, so you wouldn’t redirect them to the login page.

This code is designed to work with a regular page, and then to use the page slug.

Sorry let me try again.

If the nav links to a page at …/members-area/, do I replace that code with:

if ( is_user_logged_in() && is_page( ‘members-area’ ) ) {

or

if ( is_user_logged_in() && is_page( '/members-area'/' ) ) {

Additionally, on looking at the API/Scripting Conditionals … I see the below as a super useful way to redirect users when they arrive at the page linked to the MEMBERS navigation, if we could add a simple redirection shortcode to this – but I might be out of my mind?

 [s2If is_user_logged_in()]
    ** Redirect shorcode to members page **
[/s2If]

[s2If !is_user_logged_in()]
     ** Redirect shorcode to login page **
[/s2If]

If you are going to ask about code, you need to learn the terminology. The page slug in your example is 'members-area'. That’s what you need.

Don’t add redirects to the MOP because the user has just been redirected there. You’ll get into a redirect loop. Just put the relevant content between your first set of conditionals, and a hyperlink to the login page between the second set.

OK, so the redirect is working, but it doesn’t recognize the destination.

So you have this line, is that right?

wp_redirect( home_url( '/name-of-page/' ) );

Note that this line does not use a slug. Have you got the URL correct? By which I mean not just is there a page that ends like that, but is the URL actually mysite.com/name-of-page/?

@KTS915 Yes, I do have that line and yes, the page URL is mysite.com/name-of-page/.

The error is occurring when I edit the child theme’s functions.php file using the WP theme editor. While editing, the URL says: https://mysite.com/wp-admin/theme-editor.php?file=functions.php&theme=mytheme-child

On save, it shows a 404 with URL: https://mysite.com/wp-admin/theme-editor.php

Have now edited via CPanel editor.

@KTS915 - Forgive me if this seems a bit simple, but if a user is logged in, can we not simply redirect them to the members area if they navigate to the login page (while already logged in?)

I think you mean the Login Welcome Page (LWP).

If you do mean the LWP, that’s what you want, it’s far simpler even than that. Just make the members page the LWP!