| 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: 4.2 |
| 7 |
Author: Kevin Vess |
| 8 |
Author URI: http://vess.me/ |
| 9 |
|
| 10 |
Text Domain: wp-force-login |
| 11 |
Domain Path: /languages |
| 12 |
|
| 13 |
License: GPLv2 or later |
| 14 |
*/ |
| 15 |
|
| 16 |
/* |
| 17 |
This program is free software; you can redistribute it and/or |
| 18 |
modify it under the terms of the GNU General Public License |
| 19 |
as published by the Free Software Foundation; either version 2 |
| 20 |
of the License, or (at your option) any later version. |
| 21 |
|
| 22 |
This program is distributed in the hope that it will be useful, |
| 23 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 |
GNU General Public License for more details. |
| 26 |
|
| 27 |
You should have received a copy of the GNU General Public License |
| 28 |
along with this program; if not, write to the Free Software |
| 29 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 30 |
*/ |
| 31 |
|
| 32 |
function v_forcelogin() { |
| 33 |
|
| 34 |
// Exceptions for AJAX, Cron, or WP-CLI requests |
| 35 |
if ( ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || ( defined( 'DOING_CRON' ) && DOING_CRON ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
// Redirect unauthorized visitors |
| 40 |
if ( !is_user_logged_in() ) { |
| 41 |
// Get URL |
| 42 |
$url = isset( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ? 'https' : 'http'; |
| 43 |
$url .= '://' . $_SERVER['HTTP_HOST']; |
| 44 |
// port is prepopulated here sometimes |
| 45 |
if ( strpos( $_SERVER['HTTP_HOST'], ':' ) === FALSE ) { |
| 46 |
$url .= in_array( $_SERVER['SERVER_PORT'], array('80', '443') ) ? '' : ':' . $_SERVER['SERVER_PORT']; |
| 47 |
} |
| 48 |
$url .= $_SERVER['REQUEST_URI']; |
| 49 |
|
| 50 |
// Apply filters |
| 51 |
$whitelist = apply_filters( 'v_forcelogin_whitelist', array() ); |
| 52 |
$redirect_url = apply_filters( 'v_forcelogin_redirect', $url ); |
| 53 |
|
| 54 |
// Redirect visitors |
| 55 |
if ( preg_replace('/\?.*/', '', $url) != preg_replace('/\?.*/', '', wp_login_url()) && !in_array($url, $whitelist) ) { |
| 56 |
wp_safe_redirect( wp_login_url( $redirect_url ), 302 ); exit(); |
| 57 |
} |
| 58 |
} |
| 59 |
else { |
| 60 |
// Only allow Multisite users access to their assigned sites |
| 61 |
if ( function_exists('is_multisite') && is_multisite() ) { |
| 62 |
global $current_user; get_currentuserinfo(); |
| 63 |
if ( !is_user_member_of_blog( $current_user->ID ) && !is_super_admin() ) |
| 64 |
wp_die( __( "You're not authorized to access this site.", 'wp-force-login' ), get_option('blogname') . ' › ' . __( "Error", 'wp-force-login' ) ); |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
add_action('init', 'v_forcelogin'); |