Tự động xóa cache Cloudflare khi update bài viết WordPress

Hầu hết các website tôi sử dụng thông qua proxy Cloudflare và có set security rule để nâng cao bảo mật và cache rules everything để tối ưu tốc độ. Tuy nhiên, việc cache toàn bộ nội dung cũng tạo ra một thách thức: mỗi khi bạn cập nhật nội dung (bài viết, trang, theme, plugin), cache cũ vẫn được phục vụ cho người dùng, khiến họ không thể thấy những thay đổi mới nhất.

Thay vì phải thủ công vào dashboard Cloudflare để xóa cache mỗi lần có update, chúng ta có thể tự động hóa quá trình này bằng cách sử dụng Cloudflare API. Điều này đặc biệt hữu ích khi bạn có nhiều website hoặc thường xuyên cập nhật nội dung.

Tôi thích cách tiếp cận này vì không cần plugin, nhưng nếu bạn không rành kỹ thuật thì có lẽ hơi mất 1 ít thời gian để thao tác, trong bài viết tiếp theo tôi sẽ share plugin để bạn dễ thao tác hơn nhé.

Nếu bạn chưa biết là gì và lợi ích thế nào có thể xem qua

Tại sao cần xóa cache tự động?

  • Luôn hiển thị nội dung mới nhất cho người dùng
  • Tiết kiệm thời gian – không phải vào dashboard Cloudflare
  • Tự động hóa hoàn toàn – hoạt động ngầm
  • Selective clearing – chỉ xóa cache cần thiết
  • Performance tối ưu – chạy background, không ảnh hưởng tốc độ

Bước 1: Chuẩn bị thông tin Cloudflare API

Lấy Global API Key

  1. Truy cập
  2. Vào My ProfileAPI Tokens
  3. Copy Global API Key và email của bạn

Lấy Zone ID

  1. Vào dashboard của domain trong Cloudflare
  2. Sidebar phải sẽ thấy Zone ID

Bước 2: Cấu hình trong wp-config.php

Thêm 3 dòng này vào file wp-config.php (trước dòng “That’s all, stop editing!”):

// Cloudflare API Configuration
define('CLOUDFLARE_API_KEY', 'api-key-của-bạn');
define('CLOUDFLARE_EMAIL', 'email-của-bạn@example.com');
define('CLOUDFLARE_ZONE_ID', 'zone-id-của-bạn');

Lưu ý bảo mật: Cách này an toàn hơn việc hardcode API key trong code, vì wp-config.php không thể truy cập từ web.

Bước 3: Copy code vào functions.php

Thêm đoạn code sau vào file functions.php của theme:

// Hook xử lý async để xóa cache
add_action('vutruso_clear_cloudflare_cache_async', 'vutruso_clear_cloudflare_cache_async');
function vutruso_clear_cloudflare_cache_async($post_id) {
    // Kiểm tra xem có phải autosave không
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    
    // Chỉ xóa cache cho post và page
    if (!in_array(get_post_type($post_id), array('post', 'page'))) {
        return;
    }
    
    // Lấy URL của bài viết
    $post_url = get_permalink($post_id);
    
    // URL trang chủ
    $home_url = home_url('/');
    
    // Lấy thông tin Cloudflare từ wp-config.php
    $api_key = defined('CLOUDFLARE_API_KEY') ? CLOUDFLARE_API_KEY : '';
    $email = defined('CLOUDFLARE_EMAIL') ? CLOUDFLARE_EMAIL : '';
    $zone_id = defined('CLOUDFLARE_ZONE_ID') ? CLOUDFLARE_ZONE_ID : '';
    
    // Kiểm tra thông tin API
    if (empty($api_key) || empty($email) || empty($zone_id)) {
        error_log('Cloudflare: Chưa cấu hình API trong wp-config.php');
        return;
    }
    
    // Danh sách URLs cần xóa cache
    $urls = array($post_url, $home_url);
    
    // Thêm category URLs
    $categories = get_the_category($post_id);
    foreach ($categories as $category) {
        $urls[] = get_category_link($category->term_id);
    }
    
    // Thêm tag URLs (nếu có)
    $tags = get_the_tags($post_id);
    if ($tags) {
        foreach ($tags as $tag) {
            $urls[] = get_tag_link($tag->term_id);
        }
    }
    
    // Thêm archive URL
    $post_type = get_post_type($post_id);
    if ($post_type === 'post') {
        $urls[] = get_permalink(get_option('page_for_posts')); // Blog page
    }
    
    // Loại bỏ URLs trùng lặp và invalid
    $urls = array_unique(array_filter($urls));
    
    // Gọi Cloudflare API
    $response = wp_remote_post(
        "https://api.cloudflare.com/client/v4/zones/{$zone_id}/purge_cache",
        array(
            'timeout' => 30,
            'headers' => array(
                'X-Auth-Email' => $email,
                'X-Auth-Key' => $api_key,
                'Content-Type' => 'application/json',
            ),
            'body' => json_encode(array(
                'files' => $urls,
            )),
        )
    );
    
    // Kiểm tra kết quả
    if (is_wp_error($response)) {
        error_log('Cloudflare Error: ' . $response->get_error_message());
    } else {
        $body = wp_remote_retrieve_body($response);
        $data = json_decode($body, true);
        
        if (isset($data['success']) && $data['success']) {
            error_log('Cloudflare: Đã xóa cache thành công cho ' . count($urls) . ' URLs');
        } else {
            $error_msg = isset($data['errors'][0]['message']) ? $data['errors'][0]['message'] : 'Unknown error';
            error_log('Cloudflare API Error: ' . $error_msg);
        }
    }
}

// Hook khi lưu post
add_action('save_post', 'vutruso_trigger_cloudflare_cache_clear', 10, 3);
function vutruso_trigger_cloudflare_cache_clear($post_id, $post, $update) {
    // Chỉ xóa cache khi update bài đã publish
    if (!$update || $post->post_status !== 'publish') {
        return;
    }
    
    // Bỏ qua revision và autosave
    if (wp_is_post_revision($post_id) || wp_is_post_autosave($post_id)) {
        return;
    }
    
    // Kiểm tra và hủy scheduled event cũ nếu có
    $timestamp = wp_next_scheduled('vutruso_clear_cloudflare_cache_async', array($post_id));
    if ($timestamp) {
        wp_unschedule_event($timestamp, 'vutruso_clear_cloudflare_cache_async', array($post_id));
    }
    
    // Schedule xóa cache sau 30 giây
    wp_schedule_single_event(time() + 30, 'vutruso_clear_cloudflare_cache_async', array($post_id));
}

// Hook khi publish post mới (từ draft sang publish)
add_action('transition_post_status', 'vutruso_clear_cache_on_publish', 10, 3);
function vutruso_clear_cache_on_publish($new_status, $old_status, $post) {
    if ($old_status !== 'publish' && $new_status === 'publish') {
        // Bỏ qua revision
        if (wp_is_post_revision($post->ID)) {
            return;
        }
        
        // Schedule xóa cache sau 30 giây
        wp_schedule_single_event(time() + 30, 'vutruso_clear_cloudflare_cache_async', array($post->ID));
    }
}

// Hook xóa cache khi có comment mới được approve
add_action('comment_post', 'vutruso_clear_cache_on_comment', 10, 2);
add_action('wp_set_comment_status', 'vutruso_clear_cache_on_comment_status', 10, 2);

function vutruso_clear_cache_on_comment($comment_id, $comment_approved = null) {
    if ($comment_approved === 1 || $comment_approved === 'approve') {
        $comment = get_comment($comment_id);
        if ($comment && $comment->comment_post_ID) {
            wp_schedule_single_event(time() + 10, 'vutruso_clear_cloudflare_cache_async', array($comment->comment_post_ID));
        }
    }
}

function vutruso_clear_cache_on_comment_status($comment_id, $comment_status) {
    if ($comment_status === 'approve') {
        $comment = get_comment($comment_id);
        if ($comment && $comment->comment_post_ID) {
            wp_schedule_single_event(time() + 10, 'vutruso_clear_cloudflare_cache_async', array($comment->comment_post_ID));
        }
    }
}

// Function helper để xóa cache manually
function vutruso_manual_clear_cloudflare_cache($post_id = null) {
    if ($post_id) {
        wp_schedule_single_event(time() + 5, 'vutruso_clear_cloudflare_cache_async', array($post_id));
    } else {
        // Clear all cache
        vutruso_clear_all_cloudflare_cache();
    }
}

// Function để xóa toàn bộ cache
function vutruso_clear_all_cloudflare_cache() {
    $api_key = defined('CLOUDFLARE_API_KEY') ? CLOUDFLARE_API_KEY : '';
    $email = defined('CLOUDFLARE_EMAIL') ? CLOUDFLARE_EMAIL : '';
    $zone_id = defined('CLOUDFLARE_ZONE_ID') ? CLOUDFLARE_ZONE_ID : '';
    
    if (empty($api_key) || empty($email) || empty($zone_id)) {
        return false;
    }
    
    $response = wp_remote_post(
        "https://api.cloudflare.com/client/v4/zones/{$zone_id}/purge_cache",
        array(
            'timeout' => 30,
            'headers' => array(
                'X-Auth-Email' => $email,
                'X-Auth-Key' => $api_key,
                'Content-Type' => 'application/json',
            ),
            'body' => json_encode(array(
                'purge_everything' => true,
            )),
        )
    );
    
    if (is_wp_error($response)) {
        error_log('Cloudflare Error: ' . $response->get_error_message());
        return false;
    } else {
        $body = wp_remote_retrieve_body($response);
        $data = json_decode($body, true);
        
        if (isset($data['success']) && $data['success']) {
            error_log('Cloudflare: Đã xóa toàn bộ cache thành công');
            return true;
        } else {
            $error_msg = isset($data['errors'][0]['message']) ? $data['errors'][0]['message'] : 'Unknown error';
            error_log('Cloudflare API Error: ' . $error_msg);
            return false;
        }
    }
}

Tính năng chính

  • Tự động xóa cache sau 30 giây khi update bài
  • Xóa cache trang chủ + bài viết + category + tags
  • Hoạt động với post và page
  • Tránh duplicate events – hủy event cũ trước khi tạo mới
  • BẢO MẬT – không lộ API key trong code
  • Xử lý comment – xóa cache khi có comment mới
  • Helper functions – có thể gọi manual

Về vấn đề hiệu suất

Code này chạy hoàn toàn bất đồng bộ (Async)

wp_schedule_single_event(time() + 30, 'vutruso_clear_cloudflare_cache_async', array($post_id));

Cách hoạt động:

  • Code chỉ “đặt lịch” rồi kết thúc ngay (< 0.001 giây)
  • Xóa cache thực sự chạy sau 30 giây trong background
  • Không chặn quá trình lưu bài
  • Không ảnh hưởng frontend
  • Chỉ chạy khi admin update bài
  • Không chạy khi khách truy cập website

So sánh thời gian:

Hành động Thời gian
Lưu bài WordPress bình thường 1-2 giây
Lưu bài + code này 1-2 giây (giống hệt)
API Cloudflare chạy Sau 30 giây, trong background

Tài nguyên sử dụng:

  • CPU: Gần như 0%
  • RAM: < 1MB
  • Database: 2-3 queries nhỏ
  • Network: 1 HTTPS request (chạy sau, không chặn)

Tùy chỉnh nâng cao

1. Tăng thời gian delay

Nếu muốn delay lâu hơn:

// Thay 30 thành 60 giây
wp_schedule_single_event(time() + 60, 'vutruso_clear_cloudflare_cache_async', array($post_id));

2. Chỉ xóa cache cho bài quan trọng

// Thêm điều kiện chỉ xóa cache cho bài có nhiều view
$view_count = get_post_meta($post_id, 'views', true);
if ($view_count < 1000) {
    return; // Không xóa cache cho bài ít view
}

3. Xóa cache manual

// Xóa cache cho 1 bài cụ thể
vutruso_manual_clear_cloudflare_cache(123);

// Xóa toàn bộ cache
vutruso_clear_all_cloudflare_cache();

Theo dõi hoạt động

Để xem log hoạt động, check file error log của WordPress:

tail -f /path/to/your/wp-content/debug.log

Bạn sẽ thấy các message như:

Cloudflare: Đã xóa cache thành công cho 4 URLs

Kết luận

Giải pháp này:

  • Đơn giản: Chỉ cần thêm vào wp-config.php và functions.php
  • An toàn: API key được bảo vệ trong wp-config.php
  • Hiệu quả: Chạy background, không ảnh hưởng performance
  • Thông minh: Chỉ xóa cache cần thiết, có log để tracking

Code này được thiết kế tối ưu performance, chạy background, không ảnh hưởng tốc độ website. Bạn yên tâm sử dụng! 🚀

4.9/5 - (16 votes)

Từ khóa
Nếu bạn thấy bài viết có ích bạn có thể chia sẻ bài viết này. Yêu cầu thêm bài viết tại đây
Đã copy
vutruso

Vũ Trụ Số chuyên cung cấp hosting cho WordPress, dịch vụ thiết kế website, quản trị website cho doanh nghiệp, các dịch vụ bảo mật website WordPress, tăng tốc website WordPress

Bài viết liên quan