Trong bài viết này, chúng ta sẽ hướng dẫn cách tạo một chức năng điểm danh cho bài viết trên WordPress. Mục đích của chức năng này là yêu cầu người đọc điểm danh đủ một số ngày nhất định trước khi có thể đọc nội dung bài viết.
Đây là một tính năng hữu ích cho các khóa học trực tuyến, hệ thống bài viết giới hạn, hoặc các trang web yêu cầu sự tham gia của người dùng trong một thời gian nhất định. Bạn có thể thêm tính năng này vào website WordPress của mình với các bước đơn giản dưới đây.
Bước 1: Tạo Trường Nhập Liệu Cho Bài Viết
Đầu tiên, chúng ta cần tạo các trường nhập liệu trong trang chỉnh sửa bài viết để người quản trị có thể chỉ định yêu cầu điểm danh và số ngày điểm danh.
Code thêm trường nhập liệu “Điểm danh yêu cầu” và “Số ngày điểm danh”:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | function add_attendance_meta_boxes() { add_meta_box( 'attendance_meta_box', // ID 'Điểm danh yêu cầu', // Tiêu đề 'attendance_meta_box_callback', // Callback 'post', // Các loại bài viết (ở đây là bài viết) 'side', // Vị trí (ở cột bên phải) 'high' // Độ ưu tiên ); } add_action('add_meta_boxes', 'add_attendance_meta_boxes'); function attendance_meta_box_callback($post) { // Lấy giá trị cũ (nếu có) $attendance_required = get_post_meta($post->ID, '_attendance_required', true); $attendance_days = get_post_meta($post->ID, '_attendance_days', true); ?> <label for="attendance_required"> <input type="checkbox" name="attendance_required" id="attendance_required" value="1" <?php checked($attendance_required, '1'); ?>> Yêu cầu điểm danh để đọc bài viết này </label><br> <label for="attendance_days">Số ngày điểm danh:</label> <input type="number" name="attendance_days" id="attendance_days" value="<?php echo esc_attr($attendance_days); ?>" min="1" <?php echo $attendance_required ? '' : 'disabled'; ?>><br> <?php } // Lưu giá trị khi lưu bài viết function save_attendance_meta($post_id) { if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return; if (!current_user_can('edit_post', $post_id)) return; // Lưu giá trị checkbox và số ngày $attendance_required = isset($_POST['attendance_required']) ? '1' : '0'; update_post_meta($post_id, '_attendance_required', $attendance_required); if (isset($_POST['attendance_days'])) { update_post_meta($post_id, '_attendance_days', intval($_POST['attendance_days'])); } } add_action('save_post', 'save_attendance_meta'); |
Bước 2: Thêm SweetAlert2 Để Cải Thiện Giao Diện
Để làm cho quá trình điểm danh trở nên sinh động và dễ sử dụng hơn, chúng ta sẽ sử dụng thư viện SweetAlert2 để hiển thị các thông báo và xác nhận.
Code thêm SweetAlert2 vào WordPress:
1 2 3 4 | function enqueue_sweetalert2() { wp_enqueue_script('sweetalert2', 'https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.all.min.js', array(), null, true); } add_action('wp_enqueue_scripts', 'enqueue_sweetalert2'); |
Bước 3: Xử Lý Điểm Danh Với AJAX
Chúng ta sẽ tạo một hệ thống điểm danh bằng AJAX, để khi người dùng nhấn nút Điểm danh, hệ thống sẽ ghi nhận ngày điểm danh của họ mà không cần tải lại trang.
Code xử lý điểm danh với AJAX:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | function mark_attendance_script() { ?> <script> jQuery(document).ready(function($) { // Khi người dùng nhấn nút "Điểm danh" $('#attendance_button').on('click', function(e) { e.preventDefault(); var post_id = <?php echo get_the_ID(); ?>; var ajax_url = "<?php echo admin_url('admin-ajax.php'); ?>"; // Kiểm tra nếu người dùng chưa đăng nhập if (!<?php echo get_current_user_id(); ?>) { Swal.fire('Thông báo', 'Bạn cần đăng nhập để điểm danh', 'warning'); return; } // Hiển thị SweetAlert2 để xác nhận điểm danh Swal.fire({ title: 'Xác nhận điểm danh', text: 'Bạn muốn điểm danh để đọc bài viết này?', icon: 'question', showCancelButton: true, confirmButtonText: 'Điểm danh', cancelButtonText: 'Hủy' }).then((result) => { if (result.isConfirmed) { // Nếu xác nhận, gửi AJAX yêu cầu điểm danh $.ajax({ url: ajax_url, type: 'POST', data: { action: 'mark_attendance', post_id: post_id, }, success: function(response) { if (response.success) { Swal.fire('Thành công!', 'Bạn đã điểm danh thành công!', 'success').then(() => { location.reload(); }); } else { Swal.fire('Lỗi', response.data.message, 'error'); } } }); } }); }); }); </script> <?php } add_action('wp_footer', 'mark_attendance_script'); // AJAX xử lý điểm danh function mark_attendance_via_ajax() { if (!is_user_logged_in()) { wp_send_json_error(array('message' => 'Bạn cần đăng nhập để điểm danh.')); } if (isset($_POST['post_id']) && is_numeric($_POST['post_id'])) { $post_id = intval($_POST['post_id']); $user_id = get_current_user_id(); // Kiểm tra số ngày điểm danh yêu cầu $attendance_days = get_post_meta($post_id, '_attendance_days', true); if ($attendance_days) { // Kiểm tra xem người dùng đã điểm danh đủ số ngày chưa $user_attendance = get_user_meta($user_id, '_attendance_' . $post_id, true); if ($user_attendance && count($user_attendance) >= $attendance_days) { wp_send_json_error(array('message' => 'Bạn đã điểm danh đủ số ngày để đọc bài viết này.')); return; } // Kiểm tra xem người dùng đã điểm danh hôm nay chưa $current_date = date('Y-m-d'); if (in_array($current_date, $user_attendance)) { wp_send_json_error(array('message' => 'Bạn đã điểm danh hôm nay rồi. Vui lòng quay lại vào ngày mai để tiếp tục.')); return; } // Gọi hàm để theo dõi điểm danh của người dùng track_user_attendance($user_id, $post_id); wp_send_json_success(); } else { wp_send_json_error(array('message' => 'Bài viết này không yêu cầu điểm danh.')); } } wp_send_json_error(array('message' => 'Lỗi không xác định.')); } add_action('wp_ajax_mark_attendance', 'mark_attendance_via_ajax'); // Theo dõi điểm danh người dùng function track_user_attendance($user_id, $post_id) { $attendance_days = get_post_meta($post_id, '_attendance_days', true); if (!$attendance_days) return; $user_attendance = get_user_meta($user_id, '_attendance_' . $post_id, true); if (!$user_attendance) { $user_attendance = array(); } $current_date = date('Y-m-d'); if (!in_array($current_date, $user_attendance)) { $user_attendance[] = $current_date; update_user_meta($user_id, '_attendance_' . $post_id, $user_attendance); } } |
Bước 4: Kiểm Tra Quyền Truy Cập Bài Viết
Cuối cùng, chúng ta cần kiểm tra xem người dùng đã điểm danh đủ số ngày hay chưa, và chỉ cho phép họ truy cập vào nội dung bài viết nếu đủ điều kiện.
Code kiểm tra quyền truy cập bài viết:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | function check_if_can_access_post($content) { if (is_single()) { global $post; $user_id = get_current_user_id(); $attendance_days = get_post_meta($post->ID, '_attendance_days', true); if ($attendance_days) { if (!$user_id) { return '<p style="color: red; font-weight: bold;">Bạn cần đăng nhập để điểm danh và đọc bài viết này.</p>'; } $user_attendance = get_user_meta($user_id, '_attendance_' . $post->ID, true); $current_date = date('Y-m-d'); if ($user_attendance && count($user_attendance) >= $attendance_days) { return $content; } else { $remaining_days = $attendance_days - count($user_attendance); $history = implode(", ", $user_attendance); return ' <style> .attendance-status { background-color: #f9f9f9; border: 1px solid #ddd; padding: 20px; margin: 20px 0; border-radius: 8px; } .attendance-status h3 { color: #333; font-size: 1.2em; margin-bottom: 10px; } .attendance-history { width: 100%; border-collapse: collapse; margin-bottom: 10px; } .attendance-history th, .attendance-history td { padding: 8px 12px; border: 1px solid #ddd; text-align: left; } .attendance-history th { background-color: #f2f2f2; } .attendance-btn { display: inline-block; padding: 10px 20px; background-color: #0073e6; color: white; text-decoration: none; border-radius: 5px; margin-top: 10px; } .attendance-btn:hover { background-color: #005bb5; } </style> <div class="attendance-status"> <h3>Lịch sử điểm danh của bạn:</h3> <table class="attendance-history"> <thead> <tr> <th>Ngày điểm danh</th> </tr> </thead> <tbody> <tr> <td>' . str_replace(", ", '</td></tr><tr><td>', $history) . '</td> </tr> </tbody> </table> <p>Bạn cần điểm danh <strong>' . $remaining_days . ' ngày nữa</strong> để đọc nội dung này.</p> <a href="#" id="attendance_button" class="attendance-btn">Điểm danh ngay</a> </div>'; } } } return $content; } add_filter('the_content', 'check_if_can_access_post'); |
Tổng Kết
Với đoạn mã trên, bạn đã hoàn thành việc tích hợp tính năng điểm danh vào bài viết WordPress. Người dùng sẽ phải điểm danh đủ số ngày yêu cầu trước khi có thể đọc nội dung bài viết, và có thể dễ dàng kiểm tra lịch sử điểm danh của mình. Chúc các bạn thành công nhé.
- Cách Vô Hiệu Hóa Trường Phiếu Giảm Giá WooCommerce
- Hướng Dẫn Code Chức Năng Xác Thực Số Điện Thoại Người Dùng Trong WordPress Từ A Đến Z
- Hướng Dẫn Cách Ẩn Sản Phẩm Khi Hết Thời Gian Khuyến Mãi
- Hướng Dẫn Chi Tiết Cách Chọn Nhà Cung Cấp Hosting Tốt Nhất Cho Website
- Hướng dẫn viết game “Đào vàng” trong WordPress