Code bật tắt khóa nội dung bài viết/trang

Trong bài này mình chia sẻ code khóa nội dung cơ bản, yêu cầu login mới được xem nội dung.

bnixvn doc image 4bnixvn doc image 4

Ở đây sẽ có 2 đoạn code.

1. Code khai báo nút tick khóa nội dung trong trình soạn thảo

Dĩ nhiên là phải có cái tùy chọn bật/tắt như hình minh họa bên dưới

bnixvn doc image 5bnixvn doc image 5

Code này bỏ vào functions.php của theme đang dùng nhé

// Code thêm tuỳ chọn ẩn nội dung bài viếtclasshiddencontentMetabox {private$screen = array('post','page',	);private$meta_fields = array(array('label' => 'Hide this content','id' => 'bnix_hide_content','default' => 'Ẩn nội dung bài này','type' => 'checkbox',		),	);publicfunction__construct() {add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );add_action( 'save_post', array( $this, 'save_fields' ) );	}publicfunctionadd_meta_boxes() {foreach ( $this->screen as $single_screen ) {add_meta_box('hiddencontent',__( 'Hidden Content', 'bnix' ),array( $this, 'meta_box_callback' ),$single_screen,'advanced','default'			);		}	}publicfunctionmeta_box_callback( $post ) {wp_nonce_field( 'hiddencontent_data', 'hiddencontent_nonce' );echo'Bật / tắt ẩn nội dung';$this->field_generator( $post );	}publicfunctionfield_generator( $post ) {$output = '';foreach ( $this->meta_fields as $meta_field ) {$label = '<label for="'.$meta_field['id'] .'">'.$meta_field['label'] .'</label>';$meta_value = get_post_meta( $post->ID, $meta_field['id'], true );if ( empty( $meta_value ) ) {$meta_value = $meta_field['default']; }switch ( $meta_field['type'] ) {case'checkbox':$input = sprintf('<input %s id=" % s" name="% s" type="checkbox" value="1">',$meta_value === '1' ? 'checked' : '',$meta_field['id'],$meta_field['id']						);break;default:$input = sprintf('<input %s id="%s" name="%s" type="%s" value="%s">',$meta_field['type'] !== 'color' ? 'style="width: 100%"' : '',$meta_field['id'],$meta_field['id'],$meta_field['type'],$meta_value					);			}$output.=$this->format_rows( $label, $input );		}echo'<table class="form-table"><tbody>'.$output.'</tbody></table>';	}publicfunctionformat_rows( $label, $input ) {return'<tr><th>'.$label.'</th><td>'.$input.'</td></tr>';	}publicfunctionsave_fields( $post_id ) {if ( ! isset( $_POST['hiddencontent_nonce'] ) )return$post_id;$nonce = $_POST['hiddencontent_nonce'];if ( !wp_verify_nonce( $nonce, 'hiddencontent_data' ) )return$post_id;if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )return$post_id;foreach ( $this->meta_fields as $meta_field ) {if ( isset( $_POST[ $meta_field['id'] ] ) ) {switch ( $meta_field['type'] ) {case'email':$_POST[ $meta_field['id'] ] = sanitize_email( $_POST[ $meta_field['id'] ] );break;case'text':$_POST[ $meta_field['id'] ] = sanitize_text_field( $_POST[ $meta_field['id'] ] );break;				}update_post_meta( $post_id, $meta_field['id'], $_POST[ $meta_field['id'] ] );			} elseif ( $meta_field['type'] === 'checkbox' ) {update_post_meta( $post_id, $meta_field['id'], '0' );			}		}	}}if (class_exists('hiddencontentMetabox')) {newhiddencontentMetabox;};

2. Code khóa nội dung

Tiếp theo là code kiểm tra xem bài viết có tick vào ô khóa nội dung chưa. Có tick thì sẽ khóa. Nếu không tick/đã đăng nhập thì không khóa nội dung nữa

add_filter( 'the_content', 'hide_content' );functionhide_content($content) {if(is_singular('post') || is_page()) {$hide = get_post_meta(get_the_ID(),'bnix_hide_content', true);if(is_user_logged_in() || !$hide) {return$content;		}else {$box = '<div class="pc_warn_box">						Bạn phải đăng nhập để được xem nội dung						<div class="pc_warn_box_btn_wrap">													<a class="pc_warn_box_btn pc_login_trig" href="/wp-login.php?redirect_to='.urlencode(get_permalink()).'"><i class="fas fa-key"></i>Đăng Nhập</a>							<a class="pc_warn_box_btn pc_registr_trig" href="/dang-ky"><i class="fas fa-thumbs-up"></i>Đăng Ký</a>						</div>					</div>';$box.='<style>.pc_warn_box{border-color:#ffcc47;border-style:solid;border-width:4px 4px 4px 54px;border-radius:2px;line-height:normal;padding:11px 14px;position:relative;margin:20px auto;max-width:620px;margin-left:0 !important;}.pc_warn_box:before{content:"f06a";font-family:"Font Awesome 5 Free",fontawesome;font-weight:900;position:absolute;left:-52px;top:50%;width:50px;text-align:center;height:30px;color:#fff;font-size:30px;line-height:26px;margin-top:-13px;text-shadow:2px 2px 6px rgba(100,100,100,.05);z-index:10}.pc_warn_box .pc_warn_box_btn{margin:12px 10px 1px 0!important;position:static!important;float:none!important;display:inline-block!important;text-decoration:none}.pc_warn_box_btn{background:#fefefe!important;border:1px solid #c5c5c5!important;border-radius:2px!important;box-shadow:none;color:#5f5f5f!important;padding:7px 15px!important;font-size:14px!important}.pc_warn_box i{font-size:98%;padding-right:8px}.pc_warn_box_btn:hover{background:#2d5386!important;border-color:#2d5386!important;color:#fff!important}</style>';return$box;		}	}elsereturn$content;}

Cái code này bạn cũng chèn vào functions.php của theme đang dùng nhé.

Lưu ý:

  • Trong đoạn code có đoạn <a class=”pc_warn_box_btn pc_registr_trig” href=”/dang-ky”><i class=”fas fa-thumbs-up”></i>Đăng Ký</a>, các bạn thay /dang-ky thành link đăng ký của web mình nhé. Nếu không cho đăng ký thì xóa luôn nguyên đoạn là được.
  • Icon nếu không hiện thì bạn thêm cdn fontawesome vào nhé (cách thêm google hoặc gpt nhé)

Như vậy là các bạn đã có tính năng khóa nội dung, yêu cầu phải đăng nhập mới được xem rồi.

Chúc các bạn vui vẻ!

4.8/5 - (32 bình chọn)
Đăng ký
Thông báo về
guest
0 Bình Luận
Oldest
Newest Most Voted
Inline Feedbacks
Xem tất cả
0
Rất thích suy nghĩ của bạn, hãy bình luận.x
()
x