테스트 사이트 - 개발 중인 베타 버전입니다

워드프레스 소모임이 개설되었군요 ^^

· 10년 전 · 1034 · 2

워드프레스 소모임 개설 기념으로 개인홈페이지에서 현재 사용중인 플러그인을 공개합니다.

 

워드프레스 구조상 속도가 느린데 캐시 플러그인을 통해 속도 문제는 해결할 수 있습니다.

 

제가 사용중인 캐시 플러그인은 WP Super Cache(https://wordpress.org/plugins/wp-super-cache/) 으로 여러 옵션을 제공하고 있습니다.

 

Advance 옵션을 통해 로그인 사용자도 익명사용자와 동일한 캐시로 사용할 수 있습니다. 

 

그런데 로그인한 상태임에도 익명의 캐시된 페이지를 출력함으로 로그아웃한 상태로 보이는 문제점이 있어 관련 기본 위젯을 대신해 쿠키를 통해 사용할 수 있도록 플러그인을 만들었습니다.

 

{워드프레스 경로}/plugins/kh-widget-meta/kh-widget-meta.php 에 아래 내용을 작성하면 됩니다.

 

<?

/*

Plugin Name: Kilho's Widget_Meta with Cookie

Plugin URI: http://www.kilho.net/plugins/kh-widget-meta/

Description: 기본 Widget_Meta 에 로그인 쿠키정보를 이용한 출력 모듈

Author: Oh Kilho

Version: 0.1

Author URI: http://www.kilho.net/

*/

 

define('DISPLAYNAME_COOKIE', 'wordpress_display_name');

 

add_action('set_logged_in_cookie', 'kh_set_logged_in_cookie', 10, 5);

function kh_set_logged_in_cookie($logged_in_cookie, $expire, $expireation, $user_id, $logged_in)

{

wp_set_current_user($user_id);

$user = wp_get_current_user();

setcookie(DISPLAYNAME_COOKIE, $user -> display_name, $expire, COOKIEPATH, COOKIE_DOMAIN);

}

 

add_action('wp_logout', 'kh_wp_logout');

function kh_wp_logout()

{

setcookie( DISPLAYNAME_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH,      COOKIE_DOMAIN );

}

 

class KH_Widget_Meta extends WP_Widget {

 

function KH_Widget_Meta() {

$widget_ops = array('classname' => 'widget_meta', 'description' => '쿠키를 통해 로그인폼 출력' );

parent::__construct('kh-meta', '(KH) '.__('Meta'), $widget_ops);

}

 

function widget( $args, $instance ) {

extract($args);

$title = apply_filters('widget_title', empty($instance['title']) ? __('Meta') : $instance['title'], $instance, $this->id_base);

 

echo $before_widget;

if ( $title )

echo $before_title . $title . $after_title;

?>

<ul>

<li id="outlogin_1"></li>

<li id="outlogin_2"></li>

<li><a href="<?php bloginfo('rss2_url'); ?>" title="<?php echo esc_attr(__('Syndicate this site using RSS 2.0')); ?>"><?php _e('Entries <abbr title="Really Simple Syndication">RSS</abbr>'); ?></a></li>

<li><a href="<?php bloginfo('comments_rss2_url'); ?>" title="<?php echo esc_attr(__('The latest comments to all posts in RSS')); ?>"><?php _e('Comments <abbr title="Really Simple Syndication">RSS</abbr>'); ?></a></li>

</ul>

<script type="text/javascript">

// Trim - IE8 Error : Object doesn't support this property or method

if(typeof String.prototype.trim !== 'function') 

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }

 

// http://www.w3schools.com/js/js_cookies.asp

function getCookie(cname)

{

var name = cname + "=";

var ca = document.cookie.split(';');

if(ca.length>0)

for(var i=0; i<ca.length; i++) 

{

var c = ca[i].trim();

if (c.indexOf(name)==0) return c.substring(name.length,c.length);

}

return "";

}

 

var displayname = getCookie("<?=DISPLAYNAME_COOKIE?>");

if (displayname != "")

{

document.getElementById("outlogin_1").innerHTML = "<?='<a href=\"' . admin_url() . '\">' . __('Site Admin') . '</a>'?>";

document.getElementById("outlogin_2").innerHTML = "<?='<a href=\"' . esc_url( wp_logout_url($redirect) ) . '\">' . __('Log out') . '</a>'?>";

 

}else{

document.getElementById("outlogin_1").innerHTML = "<?='<a href=\"' . esc_url( wp_registration_url() ) . '\">' . __('Register') . '</a>'?>";

document.getElementById("outlogin_2").innerHTML = "<?='<a href=\"' . esc_url( wp_login_url($redirect) ) . '\">' . __('Log in') . '</a>'?>";

}

</script>

<?php

echo $after_widget;

}

 

function update( $new_instance, $old_instance ) {

$instance = $old_instance;

$instance['title'] = strip_tags($new_instance['title']);

 

return $instance;

}

 

function form( $instance ) {

$instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );

$title = strip_tags($instance['title']);

?>

<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>

<?php

}

}

 

function kh_meta_register_widgets() {

register_widget( 'KH_Widget_Meta' );

}

 

add_action( 'widgets_init', 'kh_meta_register_widgets' );

?>

댓글 작성

댓글을 작성하시려면 로그인이 필요합니다.

로그인하기

댓글 2개

좋은팁 감사합니다~~~!
아직 코드를 이해 못하지만 워프게시판 활성화를 위해 애쓰시는 길호님 감사합니다. ^^

게시글 목록

번호 제목
671
670
669
665
664
662
651
649
648
645
643
639
635
632
629
623
622
608
598
592
591
588
587
586
585
584
583
579
578
571