Created
July 27, 2015 19:23
-
-
Save cdsalmons/24b56f12e223c9c7b493 to your computer and use it in GitHub Desktop.
Force users to login in order to view a page within your WordPress site
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
add_action('template_redirect','spcm_check_if_logged_in'); | |
function spcm_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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment