Exclude Urls by Calling the Function Hook

by admin

It is very simple to exclude any url from the cache. You can call the following function in a theme or a plugin file to exclude current page.

wpfc_exclude_current_page();

Examples

1. Excluding Posts by Age

This code adds a filter to WordPress content, executing a function that calculates the age of the post and excludes it from caching if it’s older than a specified threshold, here set to 30 days.

add_filter('the_content', 'add_post_id_before_content');

function add_post_id_before_content($content) {
    // Check if we're inside a single post and not in the admin area
    if (is_single() && !is_admin()) {
        global $post;
        $expiration_threshold_days = 30;

        // Get the post date
        $post_date = strtotime($post->post_date);
        $current_date = time();

        // Calculate the difference in days
        $days_ago = floor(($current_date - $post_date) / (60 * 60 * 24));

        // Check if the post is older than the expiration day number
        if ($days_ago > $expiration_threshold_days) {
            wpfc_exclude_current_page();
        }
    }
    
    return $content;
}

Related articles

WooCommerce Settings
WooCommerce Settings

WP Fastest Cache is compatible with WooCommerce e-commerce plugin. No extra configuration is needed.

Alternative to WP Super Cache
Alternative to WP Super Cache

At WP Fastest Cache, we’re actually thankful for WP Super Cache. They have helped introduce many people to the wonderful…

Placeholder Image
How to Show Clean Cache Button at the Toolbar to the Authors

https://www.wpfastestcache.com/features/clear-cache-link-at-the-toolbar/

Ready to get started?

Purchase your first license and see why 1,500,000+ websites globally around the world trust us.