Trang chủCode functionBài viết
Code function

Hướng dẫn thêm nút phản ánh dưới mỗi bài post/page

27/02/2025 1726 lượt xem admin Cập nhật: 04/12/2025
5/5 - (902 bình chọn)

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:

// Thêm nút phản ánh dưới mỗi bài post/page
function add_feedback_button($content) {
    if (is_single() || is_page()) {
        $content .= '<button id="open-feedback-form" class="feedback-btn">Phản ánh bài viết</button>';
    }
    return $content;
}
add_filter('the_content', 'add_feedback_button');

// Tạo popup chứa Contact Form 7
function feedback_popup_html() {
    ?>
    <div id="feedback-popup" class="feedback-popup">
        <div class="feedback-content">
            <span id="close-feedback" class="close-btn">&times;</span>
            <h3>Phản ánh bài viết</h3>
            <?php echo do_shortcode('

Lỗi: Không tìm thấy biểu mẫu liên hệ.

'); ?> </div> </div> <style> .feedback-btn { background: #ff4d4d; color: white; padding: 10px 20px; border: none; cursor: pointer; display: block; margin: 20px auto; text-align: center; border-radius: 5px; } .feedback-popup { display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0, 0, 0, 0.5); justify-content: center; align-items: center; z-index: 9999; } .feedback-content { background: white; padding: 20px; border-radius: 5px; text-align: center; width: 50%; position: relative; } .close-btn { position: absolute; top: 10px; right: 10px; font-size: 20px; cursor: pointer; } </style> <script> document.addEventListener("DOMContentLoaded", function () { var btn = document.getElementById("open-feedback-form"), popup = document.getElementById("feedback-popup"), close = document.getElementById("close-feedback"); if (btn && popup && close) { btn.addEventListener("click", () => popup.style.display = "flex"); close.addEventListener("click", () => popup.style.display = "none"); window.addEventListener("click", e => { if (e.target === popup) popup.style.display = "none"; }); } }); </script> <?php } add_action('wp_footer', 'feedback_popup_html');
  • Thay ID form CF7 (id="123") bằng ID form phản ánh của bạn.
  • Lưu lại & kiểm tra: Khi vào bài post/page, bạn sẽ thấy nút “Phản ánh bài viết”. Nhấn vào sẽ mở popup Contact Form 7.
Chào ! Bạn thấy nội dung này thế nào?
webdy.vn
webdy.vn

Tạo web nhanh dùng thử miễn phí.

Chia sẻ ↗

Bài liên quan

×