Append no-cache URL Redirect
March 24, 2025 · AI Conversations
User
append ?no-cache=1 in any page url n wordpres templayre_redicect
Assistant
To append `?no-cache=1` to **any page URL** in a WordPress site using `template_redirect`, you can hook into WordPress’s `template_redirect` action and modify the URL when necessary.
Here’s how you can do it in your theme’s `functions.php` file:
add_action('template_redirect', function () {
// Don't run in admin area or if it's already added
if (is_admin() || isset($_GET['no-cache'])) {
return;
}
// Get current URL
$current_url = (is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
// Check if there are already query parameters
$separator = (strpos($current_url, '?') !== false) ? '&' : '?';
// Append ?no-cache=1 and redirect
wp_redirect($current_url . $separator . 'no-cache=1', 301);
exit;
});
### Notes:
– This will force all **frontend** requests to include `?no-cache=1`.
– It **skips admin pages** and prevents redirect loops by checking if `no-cache` is already present.
– Use `301` (permanent) or `302` (temporary) redirect depending on your case.
Let me know if you want this only for specific page types like posts or archives.