Skip to content

Instantly share code, notes, and snippets.

@chwnam
Created May 20, 2020 08:03
Show Gist options
  • Save chwnam/13212f17fd01ea6a8e670bd3b34b3817 to your computer and use it in GitHub Desktop.
Save chwnam/13212f17fd01ea6a8e670bd3b34b3817 to your computer and use it in GitHub Desktop.
Always Logged In
<?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>';
}
@chwnam
Copy link
Author

chwnam commented May 20, 2020

wp-config.php 에 아래 상수를 정의하여 제어할 수 있습니다.

  • ALI_USER: 필수. 로그인할 유저의 user_login
  • ALI_ENABLED: 필수. 참값으로 해야 동작.
  • ALI_REDIRECT: 선택. 이 플러그인에 의해 로그인 처리되었을 때 이동될 경로. 기본으로 /wp-admin/index.php

필터 'ali_condition'을 사용하여 조건을 변경할 수 있습니다.

관리자 화면에서 플러그인이 사용중이라는 알림이 항상 보이게 됩니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment