Thêm shortcode theo ví dụ dưới vào post, page cần khóa
[lock_content] Đây là nội dung hiển thị trước khi bị khóa. <!--more--> Đây là nội dung bị khóa. Bạn cần nhập mật khẩu để đọc tiếp. [/lock_content]
Mở file functions.php trong child theme của bạn (nếu chưa có child theme, bạn nên tạo child theme để tránh mất thay đổi khi update theme). Thêm đoạn code sau:
//đặt pass cho post
// Thêm meta box nhập mật khẩu
function add_password_meta_box() {
add_meta_box(
'custom_password_meta',
'Mật khẩu đọc thêm',
'render_password_meta_box',
['post', 'page'], // Áp dụng cho cả post và page
'side',
'default'
);
}
add_action('add_meta_boxes', 'add_password_meta_box');
function render_password_meta_box($post) {
// Lấy giá trị mật khẩu hiện tại
$password = get_post_meta($post->ID, '_custom_password', true);
?>
<label for="custom_password">Mật khẩu:</label>
<input type="password" id="custom_password" name="custom_password" value="<?php echo esc_attr($password); ?>" style="width: 100%;" />
<?php
}
// Lưu mật khẩu khi cập nhật post/page
function save_password_meta_box($post_id) {
if (isset($_POST['custom_password'])) {
update_post_meta($post_id, '_custom_password', sanitize_text_field($_POST['custom_password']));
}
}
add_action('save_post', 'save_password_meta_box');
//function xử lý shortcode hiển thị nội dung bị khóa
function lock_content_shortcode($atts, $content = null) {
if (!is_singular(['post', 'page'])) {
return $content; // Chỉ áp dụng cho post hoặc page
}
global $post;
$post_id = $post->ID;
// Lấy mật khẩu từ meta field
$stored_password = get_post_meta($post_id, '_custom_password', true);
// Xử lý cảnh báo nếu nhập sai mật khẩu
$error_message = '';
if (isset($_POST['unlock_password'])) {
if ($_POST['unlock_password'] !== $stored_password) {
$error_message = 'Mật khẩu nhập không chính xác. Vui lòng thử lại.';
}
}
// Tách nội dung thành hai phần dựa trên `<!--more-->`
$split_content = explode('<!--more-->', $content);
$visible_content = isset($split_content[0]) ? $split_content[0] : '';
$hidden_content = isset($split_content[1]) ? $split_content[1] : '';
// CSS cho giao diện khóa nội dung
$inline_css = '
<style>
.locked-content {
position: relative;
background: #e8f5e9; /* Xanh lá nhạt */
padding: 15px;
border: 1px solid #c8e6c9; /* Xanh lá nhạt đậm hơn */
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
.error-message {
color: #e53935; /* Màu đỏ cảnh báo */
font-weight: bold;
margin-bottom: 10px;
}
.lock-message {
margin-top: 10px;
background: #f1f8e9; /* Nền xanh lá rất nhạt */
padding: 15px;
text-align: center;
border-radius: 5px;
border: 1px dashed #aed581; /* Xanh lá nhạt */
}
.lock-message input {
margin-right: 5px;
padding: 8px;
border: 1px solid #c5e1a5; /* Xanh lá nhạt */
border-radius: 4px;
font-size: 14px;
width: 60%;
}
.unlock-button {
background: linear-gradient(45deg, #81c784, #66bb6a); /* Hiệu ứng chuyển màu xanh lá */
color: #fff;
border: none;
border-radius: 4px;
padding: 10px 20px;
font-size: 14px;
font-weight: bold;
text-transform: uppercase;
cursor: pointer;
transition: all 0.3s ease;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.unlock-button:hover {
background: linear-gradient(45deg, #66bb6a, #4caf50); /* Tăng đậm khi hover */
box-shadow: 0 6px 10px rgba(0, 0, 0, 0.15);
transform: translateY(-2px);
}
</style>';
// Nếu chưa nhập hoặc nhập sai mật khẩu, hiển thị nội dung bị khóa
if (!isset($_POST['unlock_password']) || $_POST['unlock_password'] !== $stored_password) {
ob_start();
echo $inline_css; // Chèn CSS ngay tại đây
?>
<div class="locked-content">
<?php echo wp_kses_post($visible_content); ?>
<div class="lock-message">
<?php if (!empty($error_message)) : ?>
<p class="error-message"><?php echo esc_html($error_message); ?></p>
<?php endif; ?>
<form method="post">
<p><strong>Nội dung tiếp theo đã bị khóa.</strong></p>
<p>Nhập mật khẩu để tiếp tục xem:</p>
<input type="password" name="unlock_password" required />
<button type="submit" class="unlock-button">Mở khóa</button>
</form>
</div>
</div>
<?php
return ob_get_clean();
}
// Nếu nhập đúng mật khẩu, hiển thị toàn bộ nội dung
return $inline_css . wp_kses_post($visible_content . $hidden_content);
}
add_shortcode('lock_content', 'lock_content_shortcode');





