| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Force Login |
| 4 |
Plugin URI: http://vess.me/ |
| 5 |
Description: Easily hide your WordPress site from public viewing by requiring visitors to log in first. Activate to turn on. |
| 6 |
Version: 3.2 |
| 7 |
Author: Kevin Vess |
| 8 |
Author URI: http://vess.me/ |
| 9 |
License: GPLv2 or later |
| 10 |
*/ |
| 11 |
|
| 12 |
/* |
| 13 |
This program is free software; you can redistribute it and/or |
| 14 |
modify it under the terms of the GNU General Public License |
| 15 |
as published by the Free Software Foundation; either version 2 |
| 16 |
of the License, or (at your option) any later version. |
| 17 |
|
| 18 |
This program is distributed in the hope that it will be useful, |
| 19 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
GNU General Public License for more details. |
| 22 |
|
| 23 |
You should have received a copy of the GNU General Public License |
| 24 |
along with this program; if not, write to the Free Software |
| 25 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 26 |
*/ |
| 27 |
|
| 28 |
function v_forcelogin() { |
| 29 |
if( !is_user_logged_in() ) { |
| 30 |
// Get URL |
| 31 |
$url = isset( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ? 'https' : 'http'; |
| 32 |
$url .= '://' . $_SERVER['HTTP_HOST']; |
| 33 |
$url .= in_array( $_SERVER['SERVER_PORT'], array('80', '443') ) ? '' : ':' . $_SERVER['SERVER_PORT']; |
| 34 |
$url .= $_SERVER['REQUEST_URI']; |
| 35 |
|
| 36 |
// Apply filters |
| 37 |
$whitelist = apply_filters('v_forcelogin_whitelist', array()); |
| 38 |
$redirect_url = apply_filters('v_forcelogin_redirect', $url); |
| 39 |
|
| 40 |
// Redirect visitors |
| 41 |
if( preg_replace('/\?.*/', '', $url) != preg_replace('/\?.*/', '', wp_login_url()) && !in_array($url, $whitelist) ) { |
| 42 |
wp_safe_redirect( wp_login_url( $redirect_url ), 302 ); exit(); |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
add_action('init', 'v_forcelogin'); |