Created
June 21, 2013 16:10
-
-
Save isGabe/5832295 to your computer and use it in GitHub Desktop.
WordPress: force log in to view any page, with redirect to requested URL#snippet #WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Password protect a WordPress site, with a redirect to the requested URL after successful login | |
* | |
* Based on: | |
* http://wordpress.stackexchange.com/a/64999 | |
* http://kovshenin.com/2012/current-url-in-wordpress/ | |
* | |
**/ | |
//Add this bit to your theme's functions.php or include in other appropriate place | |
add_action('template_redirect','fstop_check_if_logged_in'); | |
function fstop_check_if_logged_in() | |
{ | |
if(!is_user_logged_in()) //Are they logged in? If not: | |
{ | |
// Get the requested URL | |
global $wp; | |
$requested_url = home_url( $wp->request ); | |
//Set $url to {site_url()}/wp-login.php?redirect_to={$requested_url} | |
$url = add_query_arg( | |
'redirect_to', | |
$requested_url, | |
site_url('wp-login.php') | |
); | |
//redirect any request to {site_url()}/wp-login.php?redirect_to={$requested_url} | |
wp_redirect($url); | |
exit; | |
} | |
} | |
?> |
This is great !! One question, how to redirect to a different wp-login page?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I just tried to use this and got white scree. I found this method, which seems simpler and works:http://davidwalsh.name/wordpress-force-login