Redirect users to login if not logged in – UserPro Forums https://forum.userproplugin.com/forums/topic/redirect-users-to-login-if-not-logged-in/feed/ Thu, 02 May 2024 06:55:54 +0000 https://bbpress.org/?v=2.6.2 en-US https://forum.userproplugin.com/forums/topic/redirect-users-to-login-if-not-logged-in/#post-17773 <![CDATA[Redirect users to login if not logged in]]> https://forum.userproplugin.com/forums/topic/redirect-users-to-login-if-not-logged-in/#post-17773 Sun, 19 Jan 2014 18:16:28 +0000 Jer1479 First off, amazing job on this plugin! Well worth the price for the amount of time it just saved me. Keep up the good work!

My issue:

Not sure if I’m missing something, but I don’t see a way (within the plugin) to redirect users to the login page if they’re not already logged in. My entire site is private, so this feature is necessary for me. Before using Userpro, I was using the following function to accomplish this:

function please_log_in() {
if ( !is_user_logged_in() )
auth_redirect();
}

This now causes a redirect loop because of the redirects created by the plugin. I’ve tried adding a bunch of different scenarios to the conditional, but none seem to completely fix the problem. I know I’m missing something simple here. This is likely possible with very little code. What am I missing?

Any suggestions are greatly appreciated. Thanks again for a great product!

Jeremy

]]>
https://forum.userproplugin.com/forums/topic/redirect-users-to-login-if-not-logged-in/#post-17810 <![CDATA[Reply To: Redirect users to login if not logged in]]> https://forum.userproplugin.com/forums/topic/redirect-users-to-login-if-not-logged-in/#post-17810 Sun, 19 Jan 2014 21:29:16 +0000 thedeancorp I too am interested in knowing if I can restrict the entire site with this plugin.

PS Jeremy, I’m guessing this question won’t be answered by support until you verify your purchase by saving your envato purchase code in your profile.

]]>
https://forum.userproplugin.com/forums/topic/redirect-users-to-login-if-not-logged-in/#post-17906 <![CDATA[Reply To: Redirect users to login if not logged in]]> https://forum.userproplugin.com/forums/topic/redirect-users-to-login-if-not-logged-in/#post-17906 Mon, 20 Jan 2014 03:12:20 +0000 Jer1479 Oops. I was really close. Made a really silly syntax mistake on my earlier tries. Below is a function that works to redirect all logged out users to the login page. This function assumes that your login and registration urls are in the following format:
http://example.com/login and http://example.com/register.

(Note that these slugs are not the default that Userpro produces when it generates pages on install. Register/login pages are set up as children of the “profile” page. For example, http://example.com/profile/login )

Throw the following in your functions.php and you should be good to go…

function please_log_in() {
if ( !is_user_logged_in() && !is_page(array(‘login’, ‘register’)) )
wp_redirect( home_url() . ‘/login’);
}
add_action( ‘template_redirect’, ‘please_log_in’ );

You need to do two things. First, if your url structure is different than mine, put your slugs for your register and login pages in an array inside the !is_page() function at the end of line 2. Then add your login page slug for the redirect url where you see ‘/login’ on the 3rd line. It essentially works just like you read it above. If you are not logged in && the current page slug is not /login or /register, redirect to http://example.com/login

If your url structure is the same as mine, no modifications are needed. Just throw it in functions.php and you’re good.

Hope this helps someone.

Cheers!

Jeremy

PS thedeancorp, thanks for the heads up. Probably would have taken a little while to realize that.

]]>
https://forum.userproplugin.com/forums/topic/redirect-users-to-login-if-not-logged-in/#post-17970 <![CDATA[Reply To: Redirect users to login if not logged in]]> https://forum.userproplugin.com/forums/topic/redirect-users-to-login-if-not-logged-in/#post-17970 Mon, 20 Jan 2014 18:56:51 +0000 admin Locking an entire site homepage from guests, or entire site is being developed.

This feature is being developed into USERPRO core plugin. 🙂

]]>
https://forum.userproplugin.com/forums/topic/redirect-users-to-login-if-not-logged-in/#post-17976 <![CDATA[Reply To: Redirect users to login if not logged in]]> https://forum.userproplugin.com/forums/topic/redirect-users-to-login-if-not-logged-in/#post-17976 Mon, 20 Jan 2014 19:02:56 +0000 This will be good for when you do site maintenance as well. Bravo!

]]>
https://forum.userproplugin.com/forums/topic/redirect-users-to-login-if-not-logged-in/#post-17977 <![CDATA[Reply To: Redirect users to login if not logged in]]> https://forum.userproplugin.com/forums/topic/redirect-users-to-login-if-not-logged-in/#post-17977 Mon, 20 Jan 2014 19:10:46 +0000 admin Exactly! 🙂

]]>