Skip Navigation

[Resolved] Redirect users to login page

This support ticket is created 3 years, 8 months 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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 5 replies, has 3 voices.

Last updated by Waqar 3 years, 8 months ago.

Assisted by: Waqar.

Author
Posts
#1735653

Hi, I have been looking for a way to do this.

Lookign through the sites for answers has been foncusign at times.

Found this:
https://toolset.com/forums/topic/redirect-user-to-login-page-with-toolset-access/
but hen in another post it doenst really work the same anymore.

Others say get an additional plugin. Toolset is supposed to do everything and make it easier..

Could somone please do a video like he did maybe..? with an updated way to do it?
Just sending links to other posts doenst always help.

thank you

#1736395

Hello, the technique demonstrated in this video is still effective to redirect a guest User to some URL. One main difference now is that some people use the Block Editor to edit Content Templates instead of the classic editor, but that won't effect the outcome here. The general process is:
- Create a Content Template and place the meta refresh HTML tag in that template using a classic block in the Block Editor, or in the main template code if you are using the classic editor. Modify the URL as needed to point to your site's login page.
- Create a post group in Access Control and select the redirection Content Template to show to Guest Users who don't have permission to view this group.
- Add your restricted posts to this post group.

If this technique isn't working for your site, I'll be glad to take a closer look. Please provide login credentials here in the private reply fields and I will review your setup.

#1737231
Screenshot_7.jpg

I have also tried Peter Redirect Plugin as suggested here on the site.
Nothing seems to overwrite the "Defaults to the current URL." setting.

#1737361

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for further clarifying the requirement.

> I want to lock the entire ste and redirect all users to the login page:

- The forum ticket that you referred to earlier and the login form's settings from your screenshot won't help you with this particular requirement.

For this, you'll need some custom code, which can check the current visitor's login status and then check the current page and redirect to the login page, if it is not one of the special excluded pages.

Example of excluded pages, which should be accessible to non-logged-in visitors are:
- Login page
- Register page
- Forgot password page
- Password reset page

Example code snippet:


add_action( 'template_redirect', 'redirect_visitors_to_login' );
function redirect_visitors_to_login( $template ) {
	// if current visitor is not logged-in
	if ( !is_user_logged_in() ) {
		// list of pages to exclude from the redirect
		$excluded_page_ids = array(12, 14, 16, 18);
		// ID of the login page where visitors should be redirected to
		$login_page_id	= 12;
		// if the current page is not among the excluded pages
		if ( !is_page( $excluded_page_ids) ) {
			// redirect to the login page
			wp_redirect( get_permalink($login_page_id) );
		}
	}
	return $template;
}

Please replace 12, 14, 16, & 18 with the actual page IDs of the excluded pages on your website.

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through active theme's "functions.php" file.

Important note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1738583

I will try that.

#1741469

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Sure, please let me know how it goes and I'll wait to hear back from you.

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