PluginProbe
Force Login / 4.2
Force Login v4.2
trunk 1.1 1.2 1.3 2.0 2.1 3.0 3.1 3.2 3.3 4.0 4.1 4.2 5.0 5.1 5.1.1 5.2 5.3 5.4 5.5 5.6 5.6.1 5.6.2 5.6.3
wp-force-login / wp-force-login.php

wp-force-login.php in Force Login 4.2, at wp-force-login.php

68 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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') . ' &rsaquo; ' . __( "Error", 'wp-force-login' ) );
65 }
66 }
67 }
68 add_action('init', 'v_forcelogin');