Skip to:
Content
Pages
Categories
Search
Top
Bottom

Reply To: [Resolved] forcing users to login from the root


jazgold
Participant

@jazgold

hmmm i’m a little late on this, but i wrote a basic plugin for myself to do this. basically anyone who comes to your buddypress site will be directed to the login page, if they’re not already logged in. if they log in successfully, they will be taken to whatever page they had been trying to access ( i.e. not simply the backend editing area, or the front page ). to use it just paste it into a .php file, put it in your plugin folder, and activate

//  whenever a page is loaded, make sure the user is logged in
function catch_anons(){
	global $user_ID;
	get_currentuserinfo();
		$login_page = get_bloginfo('url') .'/wp-login.php';
			if ( '' == $user_ID && strrpos( curPageURL(), $login_page)===false ) {
					$redirect = "?redirect_to=" . urlencode(curPageURL());
				header( 'Location:' . $login_page . $redirect  );
		die();
			}
}
add_action('init','catch_anons'); 

//once they log in,send them to their original destination
function login(  ) {
$first_stop = '/';       
	//if there's no 'redirect_to' send them to "Home.php"
	if ( $_REQUEST['redirect_to'] )
		$first_stop =  $_REQUEST['redirect_to'];
		header( 'Location:' . $first_stop );
	die();
}
add_action('wp_login','login');

//build the current URL  wish i could say where i grabbed this, a long time ago...

function curPageURL() {
	$pageURL = 'http';
	if ($_SERVER["HTTPS"] == "on") {	$pageURL .= "s";	}
		$pageURL .= "<img src="smileys/irritated.gif" width="" height="" alt=":/" title=":/" class="bbcode_smiley" />/";
	if ($_SERVER["SERVER_PORT"] != "80") {
		$pageURL .= $_SERVER["SERVER_NAME"]. "<img src="smileys/irritated.gif" width="" height="" alt=":" title=":" class="bbcode_smiley" />" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
	} else {
		$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
		}
return $pageURL;
}
Skip to toolbar