본인인증을 페이지( page ) 또는 글( post )에 적용하기 > 그누커머스 팁자료실

그누커머스 팁자료실

그누커머스와 관련된 팁을 여러분들과 함께 공유하세요.
나누면 즐거움이 커집니다.

본인인증을 페이지( page ) 또는 글( post )에 적용하기 정보

본인인증을 페이지( page ) 또는 글( post )에 적용하기

본문

그누커머스에 0.3.1 버젼부터 본인인증 기능 추가되었습니다.

 

본인인증을 하는 방법은 메뉴얼 ( http://sir.co.kr/manual/gnucommerce/196 ) 를 참고해 주세요.

 

 

이 본인인증 기능을 페이지( page ) 또는 글( post ) 에 적용할수 있습니다.

 

아래는 페이지와 글에 본인인증을 체크하는 예제입니다.

 

아래 소스를 적용하려면 사용하시는 테마의 functions.php 또는 child theme 를 만드신후 functions.php 에 작성하시면 됩니다.

 

child theme 에 관한글

( http://sir.co.kr/manual/gnucommerce/156 )

 

 

먼저 페이지나 글( post )에 본인인증을 체크하는 기능을 추가하려면

 

wordpress 함수 add_meta_box 를 이용하여 입력할수 있는 요소를 추가합니다.

 

add_meta_box에 관한 문서

( https://codex.wordpress.org/Function_Reference/add_meta_box )

 

 

add_meta_box에 관한 문서를 보시면 

Examples 라고 쓰여져 있는 곳에 아래와 같이 소스가 적혀져 있는 부분이 있습니다.

 

<?php

/**
 * Adds a box to the main column on the Post and Page edit screens.
 */
function myplugin_add_meta_box() {

	$screens = array( 'post', 'page' );

	foreach ( $screens as $screen ) {

		add_meta_box(
			'myplugin_sectionid',
			__( 'My Post Section Title', 'myplugin_textdomain' ),
			'myplugin_meta_box_callback',
			$screen
		);
	}
}
add_action( 'add_meta_boxes', 'myplugin_add_meta_box' );

/**
 * Prints the box content.
 * 
 * @param WP_Post $post The object for the current post/page.
 */
function myplugin_meta_box_callback( $post ) {

	// Add a nonce field so we can check for it later.
	wp_nonce_field( 'myplugin_save_meta_box_data', 'myplugin_meta_box_nonce' );

	/*
	 * Use get_post_meta() to retrieve an existing value
	 * from the database and use the value for the form.
	 */
	$value = get_post_meta( $post->ID, '_my_meta_value_key', true );

	echo '<label for="myplugin_new_field">';
	_e( 'Description for this field', 'myplugin_textdomain' );
	echo '</label> ';
	echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="' . esc_attr( $value ) . '" size="25" />';
}

/**
 * When the post is saved, saves our custom data.
 *
 * @param int $post_id The ID of the post being saved.
 */
function myplugin_save_meta_box_data( $post_id ) {

	/*
	 * We need to verify this came from our screen and with proper authorization,
	 * because the save_post action can be triggered at other times.
	 */

	// Check if our nonce is set.
	if ( ! isset( $_POST['myplugin_meta_box_nonce'] ) ) {
		return;
	}

	// Verify that the nonce is valid.
	if ( ! wp_verify_nonce( $_POST['myplugin_meta_box_nonce'], 'myplugin_save_meta_box_data' ) ) {
		return;
	}

	// If this is an autosave, our form has not been submitted, so we don't want to do anything.
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
		return;
	}

	// Check the user's permissions.
	if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {

		if ( ! current_user_can( 'edit_page', $post_id ) ) {
			return;
		}

	} else {

		if ( ! current_user_can( 'edit_post', $post_id ) ) {
			return;
		}
	}

	/* OK, it's safe for us to save the data now. */
	
	// Make sure that it is set.
	if ( ! isset( $_POST['myplugin_new_field'] ) ) {
		return;
	}

	// Sanitize user input.
	$my_data = sanitize_text_field( $_POST['myplugin_new_field'] );

	// Update the meta field in the database.
	update_post_meta( $post_id, '_my_meta_value_key', $my_data );
}
add_action( 'save_post', 'myplugin_save_meta_box_data' );

  

위 소스을 그대로 적용하면 아래이미지와 같이 페이지나 글( post ) 입력페이지에 meta_box 가 추가되는것을 확인할 수 있습니다.

 

( meta_box ) 가 추가된 모습

1bc19b0d23797b38ca19b5bea2c2ee09_1452244854_7825.png
 

 

위의 소스를 아래와 같이 본인인증 을 체크할수 있도록 변경해 보도록 하겠습니다.

 

 

/**
 * Adds a box to the main column on the Post and Page edit screens.
 */
function myplugin_add_meta_box() {

    if( !defined('GC_NAME') || !function_exists('GC_VAR') ){   //그누커머스 플러그인이 없다면 작동하지 않습니다.
        return;
    }

    $config = GC_VAR()->config;

    //본인인증을 사용하지 않으면 출력하지 않습니다.
    if( !$config['cf_cert_use'] )
        return;

	$screens = array( 'post', 'page' );

	foreach ( $screens as $screen ) {

		add_meta_box(
			'myplugin_sectionid',
			__( '본인인증 사용', 'myplugin_textdomain' ),
			'myplugin_meta_box_callback',
			$screen,
            'side'  // default 값은 advanced 이지만 side로 옵션값을 준다면 화면 오른쪽 사이드부분으로 추가가 됩니다.
		);
	}
}
add_action( 'add_meta_boxes', 'myplugin_add_meta_box' );

/**
 * Prints the box content.
 * 
 * @param WP_Post $post The object for the current post/page.
 */
function myplugin_meta_box_callback( $post ) {

	// Add a nonce field so we can check for it later.
	wp_nonce_field( 'myplugin_save_meta_box_data', 'myplugin_meta_box_nonce' );

	/*
	 * Use get_post_meta() to retrieve an existing value
	 * from the database and use the value for the form.
	 */

    $ca_cert_use = get_post_meta( $post->ID, 'ca_cert_use', true );     //해당 post_meta의 본인인증 값을 가져옵니다.
    $ca_adult_use = get_post_meta( $post->ID, 'ca_adult_use', true );   //해당 post_meta의 성인인증 값을 가져옵니다.

    ?>
    <div class="form-field gnu_check_field">
        <label for="ca_cert_use_yes"><?php _e( '본인확인 체크', 'myplugin_textdomain' ); ?></label>
        <div>
        <input type="radio" name="ca_cert_use" value="1" id="ca_cert_use_yes" <?php if($ca_cert_use) echo 'checked="checked"'; ?>>
        <label for="ca_cert_use_yes"><?php _e('사용함', 'myplugin_textdomain'); ?></label>
        <input type="radio" name="ca_cert_use" value="0" id="ca_cert_use_no" <?php if(!$ca_cert_use) echo 'checked="checked"'; ?>>
        <label for="ca_cert_use_no"><?php _e('사용안함', 'myplugin_textdomain'); ?></label>
        </div>
    </div>

    <div class="form-field gnu_check_field">
        <label for="ca_adult_use_yes"><?php _e( '성인인증 체크', 'myplugin_textdomain' ); ?></label>
        <div>
        <input type="radio" name="ca_adult_use" value="1" id="ca_adult_use_yes" <?php if($ca_adult_use) echo 'checked="checked"'; ?>>
        <label for="ca_adult_use_yes"><?php _e('사용함', 'myplugin_textdomain'); ?></label>
        <input type="radio" name="ca_adult_use" value="0" id="ca_adult_use_no" <?php if(!$ca_adult_use) echo 'checked="checked"'; ?>>
        <label for="ca_adult_use_no"><?php _e('사용안함', 'myplugin_textdomain'); ?></label>
        </div>
    </div>
    <?php
}

/**
 * When the post is saved, saves our custom data.
 *
 * @param int $post_id The ID of the post being saved.
 */
function myplugin_save_meta_box_data( $post_id ) {

	/*
	 * We need to verify this came from our screen and with proper authorization,
	 * because the save_post action can be triggered at other times.
	 */

	// Check if our nonce is set.
	if ( ! isset( $_POST['myplugin_meta_box_nonce'] ) ) {
		return;
	}

	// Verify that the nonce is valid.
	if ( ! wp_verify_nonce( $_POST['myplugin_meta_box_nonce'], 'myplugin_save_meta_box_data' ) ) {
		return;
	}

	// If this is an autosave, our form has not been submitted, so we don't want to do anything.
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
		return;
	}

	// Check the user's permissions.
	if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {

		if ( ! current_user_can( 'edit_page', $post_id ) ) {
			return;
		}

	} else {

		if ( ! current_user_can( 'edit_post', $post_id ) ) {
			return;
		}
	}

	/* OK, it's safe for us to save the data now. */
	
	// Make sure that it is set.

    if( isset( $_POST['ca_cert_use'] ) ){
        // Update the meta field in the database.
        // 본인확인을 체크했다면 해당 post_meta 에 값을 입력합니다.
        update_post_meta( $post_id, 'ca_cert_use', sanitize_text_field( $_POST['ca_cert_use'] ) );
    }

    if( isset( $_POST['ca_adult_use'] ) ){
        // Update the meta field in the database.
        // 성인인증을 체크했다면 해당 post_meta 에 값을 입력합니다.
        update_post_meta( $post_id, 'ca_adult_use', sanitize_text_field( $_POST['ca_adult_use'] ) );
    }

}
add_action( 'save_post', 'myplugin_save_meta_box_data' );

 

 

 

위와 같이 변경하였다면 아래 이미지와 같이 오른쪽 사이드에 본인인증을 체크할수 있는 입력란 meta-box가 추가됩니다.

 

1bc19b0d23797b38ca19b5bea2c2ee09_1452249383_6574.png
 

 

그런 다음 해당 페이지나 글( post ) 에 들어갔을때 본인확인을 체크해야 되는 처리는 아래와 같이 합니다.

 

 

add_action( 'the_post', 'myplugin_the_post' );

function myplugin_the_post($post_object){

    //관리권한이 있으면 본인인증을 검사하지 않습니다.
    if ( current_user_can( 'manage_options' ) ) {
        return;
    }

    if( !defined('GC_NAME') || !function_exists('GC_VAR') ){   //그누커머스 플러그인이 없다면 작동하지 않습니다.
        return;
    }

    if( is_page() || is_single() ){     //페이지나 싱글 페이지 인 경우에만 적용합니다.
        $config = GC_VAR()->config;

        //본인인증을 사용하지 않으면 검사하지 않습니다.
        if( !$config['cf_cert_use'] )
            return;

        $ca_cert_use = get_post_meta( $post_object->ID, 'ca_cert_use', true );  //post 본인인증 체크값
        $ca_adult_use = get_post_meta( $post_object->ID, 'ca_adult_use', true );    //post 성인인증 체크값

        $mb_certify = is_user_logged_in() ? get_the_author_meta( 'mb_certify', get_current_user_id() )  :   '';     // 회원이면 본인확인을 했는지 체크합니다.
        $mb_adult = is_user_logged_in() ? get_the_author_meta( 'mb_adult', get_current_user_id() )  :   ''; // 회원이면 성인인증을 했는지 체크합니다.

        // 본인확인체크
        if($ca_cert_use && !$member['mb_certify']) {
            if( is_user_logged_in() )
                myplugin_error_msg_print(__('내 프로필에서 본인확인 후 이용해 주십시오.', GC_NAME));
            else
                myplugin_error_msg_print(__('본인확인된 로그인 회원만 이용할 수 있습니다.', GC_NAME));
        }

        // 성인인증체크
        if($ca_adult_use && !$member['mb_adult']) {
            if( is_user_logged_in() )
                myplugin_error_msg_print(__('본인확인으로 성인인증된 회원만 이용할 수 있습니다.\\n내 프로필에서 본인확인 후 이용해 주십시오.', GC_NAME));
            else
                myplugin_error_msg_print(__('본인확인으로 성인인증된 회원만 이용할 수 있습니다.', GC_NAME));

        }
    }
}

function myplugin_error_msg_print($msg){
    if( function_exists('gc_alert') ){
        gc_alert($msg);
    } else {
        wp_die($msg);
    }
}

 

 

위의 과정 다 했다면, 

해당 페이지나 글( post ) 에 본인확인 또는 성인인증에  체크가 되어 있고

관리자가 아닌 guest 또는 본인인증이나 성인인증을 하지 않는 회원이 접근하게 된다면 아래 이미지와 같이 동작합니다.

 

1bc19b0d23797b38ca19b5bea2c2ee09_1452250332_3213.png
 

 

아래는 위의 과정을 다 포함된 최종소스입니다.

예제를 테스트해 보시려면 사용하는 테마의 functions.php 나 child theme 를 만드신 후 그 안에 functions.php 에 아래 코드를 넣으시면 됩니다.

 

 

 

/**
 * Adds a box to the main column on the Post and Page edit screens.
 */
function myplugin_add_meta_box() {

    if( !defined('GC_NAME') || !function_exists('GC_VAR') ){   //그누커머스 플러그인이 없다면 작동하지 않습니다.
        return;
    }

    $config = GC_VAR()->config;

    //본인인증을 사용하지 않으면 출력하지 않습니다.
    if( !$config['cf_cert_use'] )
        return;

	$screens = array( 'post', 'page' );

	foreach ( $screens as $screen ) {

		add_meta_box(
			'myplugin_sectionid',
			__( '본인인증 사용', 'myplugin_textdomain' ),
			'myplugin_meta_box_callback',
			$screen,
            'side'  // default 값은 advanced 이지만 side로 옵션값을 준다면 화면 오른쪽 사이드부분으로 추가가 됩니다.
		);
	}
}
add_action( 'add_meta_boxes', 'myplugin_add_meta_box' );

/**
 * Prints the box content.
 * 
 * @param WP_Post $post The object for the current post/page.
 */
function myplugin_meta_box_callback( $post ) {

	// Add a nonce field so we can check for it later.
	wp_nonce_field( 'myplugin_save_meta_box_data', 'myplugin_meta_box_nonce' );

	/*
	 * Use get_post_meta() to retrieve an existing value
	 * from the database and use the value for the form.
	 */

    $ca_cert_use = get_post_meta( $post->ID, 'ca_cert_use', true );     //해당 post_meta의 본인인증 값을 가져옵니다.
    $ca_adult_use = get_post_meta( $post->ID, 'ca_adult_use', true );   //해당 post_meta의 성인인증 값을 가져옵니다.

    ?>
    <div class="form-field gnu_check_field">
        <label for="ca_cert_use_yes"><?php _e( '본인확인 체크', 'myplugin_textdomain' ); ?></label>
        <div>
        <input type="radio" name="ca_cert_use" value="1" id="ca_cert_use_yes" <?php if($ca_cert_use) echo 'checked="checked"'; ?>>
        <label for="ca_cert_use_yes"><?php _e('사용함', 'myplugin_textdomain'); ?></label>
        <input type="radio" name="ca_cert_use" value="0" id="ca_cert_use_no" <?php if(!$ca_cert_use) echo 'checked="checked"'; ?>>
        <label for="ca_cert_use_no"><?php _e('사용안함', 'myplugin_textdomain'); ?></label>
        </div>
    </div>

    <div class="form-field gnu_check_field">
        <label for="ca_adult_use_yes"><?php _e( '성인인증 체크', 'myplugin_textdomain' ); ?></label>
        <div>
        <input type="radio" name="ca_adult_use" value="1" id="ca_adult_use_yes" <?php if($ca_adult_use) echo 'checked="checked"'; ?>>
        <label for="ca_adult_use_yes"><?php _e('사용함', 'myplugin_textdomain'); ?></label>
        <input type="radio" name="ca_adult_use" value="0" id="ca_adult_use_no" <?php if(!$ca_adult_use) echo 'checked="checked"'; ?>>
        <label for="ca_adult_use_no"><?php _e('사용안함', 'myplugin_textdomain'); ?></label>
        </div>
    </div>
    <?php
}

/**
 * When the post is saved, saves our custom data.
 *
 * @param int $post_id The ID of the post being saved.
 */
function myplugin_save_meta_box_data( $post_id ) {

	/*
	 * We need to verify this came from our screen and with proper authorization,
	 * because the save_post action can be triggered at other times.
	 */

	// Check if our nonce is set.
	if ( ! isset( $_POST['myplugin_meta_box_nonce'] ) ) {
		return;
	}

	// Verify that the nonce is valid.
	if ( ! wp_verify_nonce( $_POST['myplugin_meta_box_nonce'], 'myplugin_save_meta_box_data' ) ) {
		return;
	}

	// If this is an autosave, our form has not been submitted, so we don't want to do anything.
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
		return;
	}

	// Check the user's permissions.
	if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {

		if ( ! current_user_can( 'edit_page', $post_id ) ) {
			return;
		}

	} else {

		if ( ! current_user_can( 'edit_post', $post_id ) ) {
			return;
		}
	}

	/* OK, it's safe for us to save the data now. */
	
	// Make sure that it is set.

    if( isset( $_POST['ca_cert_use'] ) ){
        // Update the meta field in the database.
        // 본인확인을 체크했다면 해당 post_meta 에 값을 입력합니다.
        update_post_meta( $post_id, 'ca_cert_use', sanitize_text_field( $_POST['ca_cert_use'] ) );
    }

    if( isset( $_POST['ca_adult_use'] ) ){
        // Update the meta field in the database.
        // 성인인증을 체크했다면 해당 post_meta 에 값을 입력합니다.
        update_post_meta( $post_id, 'ca_adult_use', sanitize_text_field( $_POST['ca_adult_use'] ) );
    }

}
add_action( 'save_post', 'myplugin_save_meta_box_data' );



add_action( 'the_post', 'myplugin_the_post' );

function myplugin_the_post($post_object){

    //관리권한이 있으면 본인인증을 검사하지 않는다.
    if ( current_user_can( 'manage_options' ) ) {
        return;
    }

    if( !defined('GC_NAME') || !function_exists('GC_VAR') ){   //그누커머스 플러그인이 없다면 작동하지 않음
        return;
    }

    if( is_page() || is_single() ){     //페이지나 싱글 페이지 인 경우에만 적용
        $config = GC_VAR()->config;

        //본인인증을 사용하지 않으면 검사하지 않는다.
        if( !$config['cf_cert_use'] )
            return;

        $ca_cert_use = get_post_meta( $post_object->ID, 'ca_cert_use', true );  //post 본인인증 체크값
        $ca_adult_use = get_post_meta( $post_object->ID, 'ca_adult_use', true );    //post 성인인증 체크값

        $mb_certify = is_user_logged_in() ? get_the_author_meta( 'mb_certify', get_current_user_id() )  :   '';     // 회원이면 본인확인을 했는지 체크
        $mb_adult = is_user_logged_in() ? get_the_author_meta( 'mb_adult', get_current_user_id() )  :   ''; // 회원이면 성인인증을 했는지 체크

        // 본인확인체크
        if($ca_cert_use && !$member['mb_certify']) {
            if( is_user_logged_in() )
                myplugin_error_msg_print(__('내 프로필에서 본인확인 후 이용해 주십시오.', GC_NAME));
            else
                myplugin_error_msg_print(__('본인확인된 로그인 회원만 이용할 수 있습니다.', GC_NAME));
        }

        // 성인인증체크
        if($ca_adult_use && !$member['mb_adult']) {
            if( is_user_logged_in() )
                myplugin_error_msg_print(__('본인확인으로 성인인증된 회원만 이용할 수 있습니다.\\n내 프로필에서 본인확인 후 이용해 주십시오.', GC_NAME));
            else
                myplugin_error_msg_print(__('본인확인으로 성인인증된 회원만 이용할 수 있습니다.', GC_NAME));

        }
    }
}

function myplugin_error_msg_print($msg){
    if( function_exists('gc_alert') ){
        gc_alert($msg);
    } else {
        wp_die($msg);
    }
}
추천
1

댓글 전체

전체 9
그누커머스 팁자료실 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1402호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT