WordPress.org

Plugin Directory

Changeset 848912


Ignore:
Timestamp:
01/31/14 17:10:52 (4 years ago)
Author:
daankortenbach
Message:

Complete rewrite to use the already existing auth_redirect hook.

Location:
redirect-to-login-if-not-logged-in/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • redirect-to-login-if-not-logged-in/trunk/dmk-redirect-if-not-logged-in.php

    r848798 r848912  
    22/** 
    33 * @package Redirect to login if not logged in 
    4  * @version 1.4 
     4 * @version 1.5 
    55 */ 
    66/* 
    77Plugin Name: Redirect to login if not logged in 
    88Plugin URI: http://wordpress.org/plugins/redirect-to-login-if-not-logged-in/ 
    9 Description: Redirect to wp-login.php if user is not logged in. 
     9Description: Redirects a user to the login page if the user is not logged in. After login the user gets redirected to the original entry page. 
    1010Author: Daan Kortenbach 
    11 Version: 1.4 
     11Version: 1.5 
    1212Author URI: http://daan.kortenba.ch/ 
    1313*/ 
    1414 
    15 add_action( 'init', 'dmk_not_loggedin_redirect' ); 
     15add_action( 'parse_request', 'dmk_redirect_to_login_if_not_logged_in', 1 ); 
     16/** 
     17 * Redirects a user to the login page if not logged in. 
     18 * 
     19 * @author Daan Kortenbach 
     20 */ 
     21function dmk_redirect_to_login_if_not_logged_in() { 
     22    is_user_logged_in() || auth_redirect(); 
     23} 
    1624 
     25 
     26add_filter( 'login_url', 'dmk_strip_loggedout', 1, 1 ); 
    1727/** 
    18  * Conditional: if user is not logged in and is not on wp-login.php, call redirect on hook 'wp'. 
     28 * Strips '?loggedout=true' from redirect url after login. 
    1929 * 
    2030 * @author Daan Kortenbach 
    2131 * 
    22  * @global string $pagenow 
    23  * @return void 
     32 * @param  string $login_url 
     33 * @return string $login_url 
    2434 */ 
    25 function dmk_not_loggedin_redirect() { 
    26     global $pagenow; 
    27  
    28     if ( ! is_user_logged_in() && $pagenow != 'wp-login.php' ) { 
    29         add_action( 'wp', 'dmk_not_loggedin_do_redirect' ); 
    30     } 
     35function dmk_strip_loggedout( $login_url ) { 
     36    return str_replace( '%3Floggedout%3Dtrue', '', $login_url ); 
    3137} 
    32  
    33 /** 
    34  * Redirect to wp-login.php. 
    35  * 
    36  * @author Daan Kortenbach 
    37  * 
    38  * @return void redirect 
    39  */ 
    40 function dmk_not_loggedin_do_redirect() { 
    41     wp_redirect( wp_login_url(), 302 ); 
    42     exit; 
    43 } 
  • redirect-to-login-if-not-logged-in/trunk/readme.txt

    r848798 r848912  
    55Requires at least: 3.0.1 
    66Tested up to: 3.8.1 
    7 Stable tag: 1.4 
     7Stable tag: 1.5 
    88License: GPLv2 or later 
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html 
     
    1313== Description == 
    1414 
    15 Redirects site visitors to the login page if the visitor is not logged in. This plugin is used fairly often for sites in development or sites only available for registered users. 
     15Redirects a user to the login page if the user is not logged in. After login the user gets redirected to the original entry page. This plugin is used fairly often for sites in development or sites only available for registered users. 
    1616 
    1717== Installation == 
     
    2121 
    2222== Changelog == 
     23 
     24= 1.5 = 
     25* Complete rewrite to use the already existing auth_redirect hook. 
     26* Strips '?loggedout=true' from redirect url after login. 
    2327 
    2428= 1.4 = 
Note: See TracChangeset for help on using the changeset viewer.