Skip to content

trepmal.com

  • Home
  • Plugins
  • Github
  • Me on WP.org
  • Donate

Categories

  • WordPress Bits (149)
  • Uncategorized (11)
  • Scripts (5)
  • Tidbits (5)
  • Fun (3)

Tags

  • wordpress (30)
  • administration (16)
  • plugin (9)
  • free download (8)
  • functions.php (8)

Year

  • 2018 (8)
  • 2016 (2)
  • 2015 (4)
  • 2014 (19)
  • 2013 (11)

Function: wp_login_url

Redirect All Logged-Out Users to Log In Screen

If you’re running a private blog and you want to make sure all logged-out visitors are redirected to the sign-in page, this will do the trick.

add_action( 'template_redirect', 'logged_in_users_only' );
function logged_in_users_only() {
	if ( !is_user_logged_in() )
	wp_redirect( wp_login_url() );
}

If you want users to come back to the front page after logging in, you can pass an argument to the wp_login_url() function.

add_action( 'template_redirect', 'logged_in_users_only' );
function logged_in_users_only() {
	if ( !is_user_logged_in() )
	wp_redirect( wp_login_url( home_url() ) );
}

This will only redirect users who were initially redirected to the login screen from the front-end. If the user went directly to the login screen – the filter above won’t have been triggered, so they’ll be taken to the administration side.

Posted on April 1, 2011March 1, 2012Categories WordPress BitsLeave a comment on Redirect All Logged-Out Users to Log In Screen
Proudly powered by WordPress