Skip to content

Instantly share code, notes, and snippets.

@marcosfreitas
Forked from isGabe/wordpress-force-login.php
Created January 30, 2014 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcosfreitas/8709966 to your computer and use it in GitHub Desktop.
Save marcosfreitas/8709966 to your computer and use it in GitHub Desktop.
<?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;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment