I am building a Digg like website in WordPress.

After installing W3 Total Cache, I noticed certain elements such as number of votes (and voters ids) per post are cached even though they shouldn't be (at least not after a user votes for an article). I assume it is not possible to prevent specific elements in a page from being cached (or is it?), so I thought of triggering page cache refresh programmatically.

Any suggestions?

link|improve this question

33% accept rate
feedback

5 Answers

W3 Total Cache supports fragment caching. From FAQ:

How do I implement fragment caching? 

Edit your templates to with the following syntax to ensure that dynamic features remain so:

Example 1:
<!-- mfunc any PHP code --><!-- /mfunc -->

Example 2:
<!-- mfunc -->any PHP code<!-- /mfunc -->

Example 3:
<!--MFUNC           -->
                                      echo rand();
<!--/mfunc -->

Example 4:
<!-- mclude path/to/file.php --><!-- /mclude -->

Example 5:
<!-- mclude -->path/to/file.php<!-- /mclude -->
link|improve this answer
Thanks Rast. But when I use any of the above examples, I get an error Parse error: syntax error, unexpected '<' – user1567 Jan 17 '11 at 14:34
@user1567 you probably have mismatch with opening/closing <?php ?> tags. I hadn't used these yet so don't have practical snippet to show. – Rarst Jan 17 '11 at 14:57
For one thing, I would like to load functions.php without caching it. The following code in functions.php triggered the error above: <?php <!-- mclude -->require_once(functions2.php)<!-- /mclude --> ?> What is wrong with the code? – user1567 Jan 17 '11 at 15:20
1  
@user1567 according to examples it should be something like ?><!-- mclude -->functions2.php<!-- /mclude --><?php (I think :) – Rarst Jan 17 '11 at 15:36
Thanks for your help. – user1567 Jan 18 '11 at 10:24
feedback

if you want to flush the cache you can do that: the plugin has functions for that

<?php 

flush_pgcache()  //page cache
flush_dbcache()  // database cache
flush_minify()  // minify cache
flush_all() //all caches

?>

and you just need to call it like this:

<?php 
 $w3_plugin_totalcache->flush_all();
?>

and that is basically the answer to the question in the title "cache refresh programmatically"

link|improve this answer
Great! thank you – user1567 Jan 17 '11 at 21:20
glad i could help! – Bainternet Jan 17 '11 at 22:17
feedback

Bainternet's solution didn't seem to work for me.

I'm successfully using this alternative snippet within a plugin, loaded at the admin_init action:

// Clear all W3 Total Cache
if( class_exists('W3_Plugin_TotalCacheAdmin') )
{
    $plugin_totalcacheadmin = & w3_instance('W3_Plugin_TotalCacheAdmin');

    $plugin_totalcacheadmin->flush_all();

    echo __('<div class="updated"><p>All <strong>W3 Total Cache</strong> caches successfully emptied.</p></div>');
}

Hopefully this helps someone out there.

link|improve this answer
I can confirm that kevinlearynet's solution works with W3 Total Cache version 0.9.2.4. It broke for my plugin after that upgrade, and this works a treat. Thank you! Paul. – user9177 Oct 4 '11 at 18:55
feedback

Use this snippet to make sure your PHP runs regardless of whether caching is on or off. Yes, you have to write/call your function twice.

<!-- mfunc echo 'caching ON'; --><?php echo 'caching OFF'; ?><!-- /mfunc -->


(I think) this is how it works:

  • The mfunc conditionals replace php tags.
  • If caching is OFF, php inside mfunc comments appear in your markup as a HTML comments. <!-- mfunc echo "hello?"; --> Keep this in mind depending on how happy you are for people to see your PHP (only happens when caching is off).
link|improve this answer
feedback

I have written a post on my blog which directly answers this topic. My post covers in detail, how to install the code, how to use the code, and what the code does.

If you read my blog post, you will be able to easily, automatically clear caches on the WordPress action hook: save_post, from the WordPress plugin: W3 Total Cache.

Automatically clear all caches with W3 Total Cache

Example usage of functions:

$this->clear_db_caches();
    Calls the W3TC function: w3tc_dbcache_flush();
    Clears the DB caches completely. Note: Doesn’t clear MySQL Query Cache, just WP based DB caches.
$this->clear_minify_caches();
    Calls the W3TC function: w3tc_minify_flush();
    Clears the minified content caches completely.
$this->clear_object_caches();
    Calls the W3TC function: w3tc_objectcache_flush();
    Clears the object caches completely.
$this->clear_page_caches();
    Calls the W3TC function: w3tc_pgcache_flush();
    Clears the Page Cache completely.
$this->clear_all_caches();
    Calls each of the W3TC functions in one instance.
    Attempts to clear all W3TC caches completely.
link|improve this answer
Add solutions, not just links. – toscho 5 hours ago
feedback

Your Answer

 
or
required, but never shown

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