Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

It is possible? How can I do that?

share|improve this question
    
Old way was with mfunc(), but there is a new way: The shiny new dynamic content in WP Super Cache. Try it out and report back in an answer below. –  webaware Dec 7 '13 at 0:41
add comment

2 Answers

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.

share|improve this answer
add comment

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 »

share|improve this answer
    
Thank you! I've tried this, doesn't change :( my list isn't dynamically generated :/ –  ranell Oct 29 '12 at 18:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.