Skip to content

Instantly share code, notes, and snippets.

@mehrshaddarzi
Created July 20, 2025 08:56
Show Gist options
  • Save mehrshaddarzi/9f05bf0947fd58ba2f4c2c37cb957185 to your computer and use it in GitHub Desktop.
Save mehrshaddarzi/9f05bf0947fd58ba2f4c2c37cb957185 to your computer and use it in GitHub Desktop.
No Cache Header in WordPress
<?php
/**
* افزودن هدرهای عدم کش برای صفحات حاوی 'rahkaran' در URL
*/
add_filter('nocache_headers_args', 'rahkaran_no_cache_headers_with_cloudflare');
function rahkaran_no_cache_headers_with_cloudflare($headers) {
// بررسی وجود 'rahkaran' در URL بدون توجه به بزرگی/کوچکی حروف
if (isset($_SERVER['REQUEST_URI']) {
$request_uri = strtolower($_SERVER['REQUEST_URI']);
if (false !== strpos($request_uri, 'rahkaran')) {
$headers = [
'Expires' => 'Wed, 11 Jan 1984 05:00:00 GMT',
'Cache-Control' => 'no-cache, no-store, must-revalidate, max-age=0, s-maxage=0',
'Pragma' => 'no-cache',
// هدرهای مخصوص Cloudflare
'CF-Cache-Status' => 'BYPASS',
'Cache-Tag' => 'no-cache',
'CDN-Cache-Control' => 'no-cache',
// هدرهای اضافی برای سایر CDNها
'Edge-Cache-Control' => 'no-cache',
'Surrogate-Control' => 'no-store',
];
}
}
return $headers;
}
/**
* غیرفعال کردن کش در پلاگین‌های کش وردپرس
*/
add_action('wp', 'disable_caching_plugins_for_rahkaran');
function disable_caching_plugins_for_rahkaran() {
if (isset($_SERVER['REQUEST_URI']) {
$request_uri = strtolower($_SERVER['REQUEST_URI']);
if (false !== strpos($request_uri, 'rahkaran')) {
// برای پلاگین‌های عمومی
if (!defined('DONOTCACHEPAGE')) {
define('DONOTCACHEPAGE', true);
}
// برای WP Rocket
add_filter('do_rocket_generate_caching_files', '__return_false');
add_filter('rocket_override_donotcachepage', '__return_true');
// برای W3 Total Cache
add_filter('w3tc_should_page_be_cached', '__return_false');
// برای LiteSpeed Cache
add_filter('litespeed_control_set_nocache', '__return_true');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment