wp-super-cache exclude file from caching

Is there any mean to exclude a *.php file to WP Super Cache?
I’ve in my single.php page an include news.php, the file news.php contains ten links to my last articles.

So I want to cache single.php but to exclude news.php that is included in single.php.

Read More

It is possible? How can I do that?

Related posts

Leave a Reply

2 comments

  1. Rather than incorporating dynamic content into cached pages, you are better off using AJAX to pull dynamic content onto those pages in the browser.

    In your news.php file, simply have an empty element that will contain the content. Better still, have it contain an AJAX spinner that indicates that content is coming.

    <div id="late-load-news">
        <img src="<?php echo get_stylesheet_directory_uri(); ?>/images/ajax-loader.gif" />
    </div>
    

    Then use jQuery to load your news content — this code goes in your theme’s functions.php:

    /**
    * AJAX handler to output an HTML fragment for news
    */
    function wpse_70813_news() {
        // stop browser from caching output
        header('Pragma: public');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
    
        //...output your news content here...
    
        exit;
    }
    
    add_action('wp_ajax_late-load-news', 'wpse_70813_news');
    add_action('wp_ajax_nopriv_late-load-news', 'wpse_70813_news');
    
    /**
    * enqueue jQuery
    */
    add_action('wp_enqueue_script', function() {
        wp_enqueue_script('jquery');
    });
    
    /**
    * add footer script to load news
    */
    add_action('wp_print_footer_script', function() {
        ?>
    
        <script>
        jQuery.ajax({
            type : "GET",
            url : "<?php echo admin_url('admin-ajax.php'); ?>",
            dataType : "html",
            data : { action: "late-load-news" },
            success : function(content) {
                jQuery("#late-load-news").html(content);
            }
        });
        </script>
    
        <?php
    });
    

    That will load the content every time a visitor navigates to a new page. If your news content doesn’t change much, I recommend you make use of sessionStorage to cache the news content in the browser for the duration of the visit. If you want to limit how long that cache lasts, see Session storage with expiry time.

  2. Follow the steps below to prevent caching of the news.php file:

    1. Visit the WP Super Cache settings page in your WordPress administration dashboard (Settings > WP Super Cache).

    2. Visit the ‘Advanced’ tab along the top of that page

    3. Scroll down to the Accepted Filenames & Rejected URIs heading

    4. Look for the box with the description of:

      Add here those filenames that can be cached, even if they match one of the rejected substring specified above.

    5. Enter news.php on a new line in the textbox below the description

    6. Click Save Files »