Created
May 20, 2020 08:03
-
-
Save chwnam/13212f17fd01ea6a8e670bd3b34b3817 to your computer and use it in GitHub Desktop.
Always Logged In
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Plugin Name: Always Logged In | |
| * Description: Make you logged in, always. | |
| * Author: changwoo | |
| * Author URI: mailto://[email protected] | |
| */ | |
| add_action( 'init', 'ali_init' ); | |
| function ali_init() { | |
| if ( ali_condition() ) { | |
| $user = get_user_by( 'login', defined( 'ALI_USER' ) ? ALI_USER : '' ); | |
| if ( $user && $user->exists() ) { | |
| wp_set_auth_cookie( $user->ID, true, is_ssl() ); | |
| $redirect = apply_filters( 'ali_redirect', defined( 'ALI_REDIRECT' ) ? ALI_REDIRECT : null ); | |
| if ( ! $redirect ) { | |
| $redirect = '/wp-admin/'; | |
| } | |
| wp_safe_redirect( esc_url_raw( $redirect ) ); | |
| exit; | |
| } | |
| } else { | |
| $user = wp_get_current_user(); | |
| if ( $user && $user->exists() ) { | |
| add_action( 'admin_notices', 'ali_notice' ); | |
| } | |
| } | |
| } | |
| function ali_condition() { | |
| /* | |
| * 1. 로컬에서 접속할 것. | |
| * 2. WP_CLI 사용 중이 아닐 것. | |
| * 3. DOING_* 류 상수 정의가 되어 있지 않을 것. | |
| * 4. ALI_USER 상수를 정의할 것. | |
| * 5. ALI_ENABLED 상수를 정의하고 참으로 할 것. | |
| * 6. 로그인되어 있지 않을 것. | |
| */ | |
| $condition = '127.0.0.1' === ( $_SERVER['REMOTE_ADDR'] ?? '' ) && | |
| ! defined( 'WP_CLI' ) && | |
| ! defined( 'DOING_CRON' ) && | |
| ! defined( 'DOING_AJAX' ) && | |
| ! defined( 'DOING_AUTOSAVE' ) && | |
| ( defined( 'ALI_USER' ) && ALI_USER ) && | |
| ( defined( 'ALI_ENABLED' ) && ALI_ENABLED ) && | |
| ! is_user_logged_in(); | |
| return apply_filters( 'ali_condition', $condition ); | |
| } | |
| function ali_notice() { | |
| echo '<div class="notice notice-info"><p>Always Logged In Must-Use plugin is active. You might never be logged out!</p></div>'; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
wp-config.php 에 아래 상수를 정의하여 제어할 수 있습니다.
필터 'ali_condition'을 사용하여 조건을 변경할 수 있습니다.
관리자 화면에서 플러그인이 사용중이라는 알림이 항상 보이게 됩니다.