Skip Navigation

[Resolved] Redirect from limited content to login form

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here i that the user wanted to redirect hiss guests from certain pages to a login page.
Solution:

This can be done by using the code below. Add it to your function.php file

function wpsites_redirect_login_page() {
    $arr = array('my-account', 'mijn-account/','add-your-first-bike','edit-bike','edit-profile','sell-bike','report-bike','add-bike','bike-reported');
    if ( ! is_user_logged_in() && is_page($arr) ) {
       wp_redirect( 'my_url' );
        exit();
    }
}
add_action( 'template_redirect', 'wpsites_redirect_login_page' );

Finally just add the slug of your page to the array and it will automatically redirect to a url that you can specify

This support ticket is created 6 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 11 replies, has 2 voices.

Last updated by JanP6468 6 years ago.

Assisted by: Shane.

Author
Posts
#686098
Capture2.jpg

Hi,

We are solving a repeated issue with a redirection to the login page (the one we already solved once is here: https://toolset.com/forums/topic/redirect-from-limited-content-to-login-form/page/2/)

Issue: when logged in or not, there should be a menu item "MY FIETS ID" visible. When logged in, this leads you to "My Account" page, when not, it redirects to login.

Shane already helped us set this up once (see the link above), but for some reason it doesn't work now (the MY FIETS ID menu item doesnt show when not logged in for some reason).

Could you please help us figure out what is the problem (and ideally also send us the PHP snippet Shane used last time, please)?

Thank you,

Jan

#686505

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jan,

Thanks for getting in touch again.

Could you provide the ftp and credentials so I can take a look at this for you ?

Is it that the script no longer works ?

Please let me know.

Thanks,
Shane

#687151

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jan,

I tried the credentials for both the ftp and wordpress login and they don't seem to work.

Could you check on this and let me know.

Thanks,
Shane

#687736

I changed the password and edited the hidden post, now the access should be ok for both.

Thanks!

Jan

#688228

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jan,

Could you disable the Access plugin and let me know if you can access the account page when you are logged out ?

Reason is because i noticed that I'm getting a page not found issue when I try to view the page so I suspect maybe the access plugin is interfering .

Please try and let me know.

Thanks,
Shane

#690804

Hi Shane,

When I deactivate the Toolset Access plugin, I can access the account page. The 404 page not found indeed is an Access redirect.

Jan

#692218

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jan,

Awesome so I was able to find the issue.

So it was that you had a post group that had the bike pages on it hidden link

What you had done was to set a 404 for guests because they don't have access. This overwrites the settings in the custom code.

For this to work the guests must have access but then the code will recognise that they are not logged in and then redirect them to log in.

It should be working fine now.

Thanks,
Shane

#693787

Awesome! It really works for English mutation. The Dutch (NL) one lets the non-logged-in users through, though. Is there any way to prevent this please?

Thank you,

Jan

#694710

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jan,

All I did was to add the slug for the translation and the redirected the page.

function wpsites_redirect_login_page() {
	$arr = array('my-account', 'mijn-account/','add-your-first-bike','edit-bike','edit-profile','sell-bike','report-bike','add-bike','bike-reported');
    if ( ! is_user_logged_in() && is_page($arr) ) {
       wp_redirect( '<em><u>hidden link</u></em>' );
        exit();
    }
}
add_action( 'template_redirect', 'wpsites_redirect_login_page' );

Thanks,
Shane

#696511

Hi,

Thanks so much, Shane. It seems like you added only the translation for "My Account" though, would you please also include the slugs for all the other translated pages (plus one missing in EN)? I have no clue how to access the code (I was always only using the PHP snippets plugin). These are the slugs (already in the right format I hope):

'bike-submitted-for-sale','fiets-ingediend-voor-verkoop','voeg-je-eerste-fiets-toe','wijzig-fiets','wijzig-profiel','verkoop-fiets','rapporteer-fiets','voeg-nieuwe-fiets-toe','fiets-gemeld'

Thank you,

Jan

#697936

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jan,

I'm happy I was able to help.

Actually you can add it by going here
hidden link

I was just adding the missing slugs to the snippet there.

Thanks,
Shane

#726815

Thank you Shane! I saw it is working now even without adding the slugs into the snippet, but I added them anyway, and next time I can edit those myself easily.

Many thanks for your help!

Jan

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.