21

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?

21

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"

  • 12
    Can you please explicitly define how you instantiated the $w3_plugin_totalcache object? – adamdport Feb 27 '14 at 15:35
  • 3
    Partially helpful, how did you instantiate that variable? – thephpdev Jan 23 '17 at 12:29
10

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 -->
  • 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
  • Not able to get to work fragment caching. My issue described here: stackoverflow.com/questions/31125413/… – JackTheKnife Jun 30 '15 at 16:38
10

To flush a single page by post id in w3tc v0.9.3 I found this worked:

if (function_exists('w3tc_pgcache_flush_post')){
 w3tc_pgcache_flush_post($post_id);
}
  • 1
    Where would you place this code? This looks like it runs on every page load (for that ID). Is there no way to prevent to caching completely? – s_ha_dum Mar 20 '14 at 22:31
  • @s_ha_dum Could you find out with which hook we can use this code ? – Prafulla Kumar Sahu Nov 17 '16 at 8:42
  • Extra hooks are not needed if the event your custom code/plugin attaches to executes after plugins have loaded, which is typically the case. All available functions are in w3-total-cache-api.php in the W3TC's plugin dir. – Walf Aug 1 '18 at 2:29
  • Thank you, this was helpful for me now in Dec 2018. I needed a way to flush the cache of the home page once the client visited a certain page (a settings page to display when an agent is available). This is very useful for specific pages, but it would be nice if there was a function that flushed ALL caches like the Purge All Caches does! – Jordan Carter Dec 1 '18 at 21:47
9

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.

  • 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
  • 2
    I'm trying to run this from the command line. Can I ask how you loaded the W3_Plugin_TotalCacheAdmin class? I tried requiring wp-load.php to no avail. – adamdport Feb 26 '14 at 22:56
5

None of the above worked for me in my plugin. However this worked! Confirmed working for version 0.9.2.4 of w3tc.

if (function_exists('w3tc_dbcache_flush')) { w3tc_dbcache_flush(); }

I did a " $wpdb->get_results( "SELECT sb_settings.f_fb_app_id,sb_settings.f_fb_secret FROM sb_settings" ); " and was surprised the values f_fb_secret and f_fb_app_id was the same every time. It was obviously the w3tc cacheing the result of the query. So I added a dbcache flush in my modify page for the sb_settings table.

Should you want to clear page cache, then just use w3tc_pgcache_flush instead.

  • I'd like the downvoter to say why he downvoted. Is there anything wrong in my answer? – Elrinth Apr 12 '13 at 8:19
0

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).

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