Title: Flush W3TC Cache Programmatically Doesn&#8217;t Work
Last modified: October 8, 2018

---

# Flush W3TC Cache Programmatically Doesn’t Work

 *  [jaypabs](https://wordpress.org/support/users/jaypabs/)
 * (@jaypabs)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/flush-w3tc-cache-programmatically-doesnt-work/)
 * I have a plugin that uses a shortcode and the shortcode has a value that needs
   to be change. I want that after the value of the shortcode changed, the post 
   that uses that shortcode flushes its cache so the changes will take effect after
   the change.
 * I’ve added the following code to flush the single post without luck.
 *     ```
       if (function_exists('w3tc_flush_post')){
           w3tc_flush_post($post_id);
       }
       ```
   
 * I’ve also tried to use the following code to flush all cache without luck as 
   will.
 *     ```
       if (function_exists('w3tc_flush_all')){
           w3tc_flush_all();
       }
       ```
   
 * Is there something I need to initialize before calling this function?

Viewing 9 replies - 1 through 9 (of 9 total)

 *  Plugin Contributor [Marko Vasiljevic](https://wordpress.org/support/users/vmarko/)
 * (@vmarko)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/flush-w3tc-cache-programmatically-doesnt-work/#post-10759516)
 * Hello [@jaypabs](https://wordpress.org/support/users/jaypabs/),
 * The first code should work just fine. The fact that it doesn’t work might be 
   caused by some error which should be logged in PHP logs.
 *  Thread Starter [jaypabs](https://wordpress.org/support/users/jaypabs/)
 * (@jaypabs)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/flush-w3tc-cache-programmatically-doesnt-work/#post-10760210)
 * Hi Marko,
 * Thank you for the reply. I can’t find any error in my php log. I already enabled
   debugging in wp-config.php.
 * The second code doesn’t work also. It really doesn’t need any initialization 
   to call w3tc_flush_post or w3tc_flush_all function?
 *  Thread Starter [jaypabs](https://wordpress.org/support/users/jaypabs/)
 * (@jaypabs)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/flush-w3tc-cache-programmatically-doesnt-work/#post-10765807)
 * Hi Marko,
 * Did you test it on your end? If you can double check that it’s working, please
   do let me know. Because I’ve already tried it myself several times and it’s not
   really working.
 *  Plugin Contributor [Marko Vasiljevic](https://wordpress.org/support/users/vmarko/)
 * (@vmarko)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/flush-w3tc-cache-programmatically-doesnt-work/#post-10766386)
 * Hello,
 * That function is already defined
 * You only add the `w3tc_flush_post($post_id)` call where you wants pages to be
   flushed
 *  Thread Starter [jaypabs](https://wordpress.org/support/users/jaypabs/)
 * (@jaypabs)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/flush-w3tc-cache-programmatically-doesnt-work/#post-10766467)
 * Hi Marko,
 * Did you try it or double check if it’s working?
 *  Plugin Contributor [Marko Vasiljevic](https://wordpress.org/support/users/vmarko/)
 * (@vmarko)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/flush-w3tc-cache-programmatically-doesnt-work/#post-10766535)
 * Hello [@jaypabs](https://wordpress.org/support/users/jaypabs/),
 * This is the correct call. Where did you add the code?
 *  Thread Starter [jaypabs](https://wordpress.org/support/users/jaypabs/)
 * (@jaypabs)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/flush-w3tc-cache-programmatically-doesnt-work/#post-10769354)
 * Hi Marko,
 * I put the code in my plugin behind the update button. When the update button 
   is clicked, the following code will be called:
 *     ```
       if (isset($_POST['edit_result']))
       {
           ...
   
       	if (function_exists('w3tc_flush_post')){
       		w3tc_flush_post($post_id);
       	}
   
           /*if ( function_exists( 'w3tc_pgcache_flush' ) ) { 
               w3tc_pgcache_flush(); 
           }*/
   
       	...
   
       }
       ```
   
 * Thanks for the help.
 *  Plugin Contributor [Marko Vasiljevic](https://wordpress.org/support/users/vmarko/)
 * (@vmarko)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/flush-w3tc-cache-programmatically-doesnt-work/#post-10770361)
 * Hello,
 * If the shortcode is on a page, and if you change the shortcode and to publish
   that, you need to click the update button on the page you are editing. We already
   have the Page Cache Purge Policy for that.
    If you’re using the shortcode in 
   a widget, then there’s an action/filter to fire a callback after the update button
   on the widget is clicked, but there is no post_id there, so you cannot use `w3tc_flush_post`
   that way.
 *  Thread Starter [jaypabs](https://wordpress.org/support/users/jaypabs/)
 * (@jaypabs)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/flush-w3tc-cache-programmatically-doesnt-work/#post-10770430)
 * Hi,
 * Yes, the shortcode is on a page. When we change the value of the shortcode and
   after clicking the update button, it fires this code: w3tc_flush_post($post_id);
 * But it’s not working. BTW, what’s the action/filter to fire a callback when the
   update button is clicked?

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Flush W3TC Cache Programmatically Doesn’t Work’ is closed to new replies.

 * ![](https://ps.w.org/w3-total-cache/assets/icon-256x256.png?rev=1041806)
 * [W3 Total Cache](https://wordpress.org/plugins/w3-total-cache/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/w3-total-cache/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/w3-total-cache/)
 * [Active Topics](https://wordpress.org/support/plugin/w3-total-cache/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/w3-total-cache/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/w3-total-cache/reviews/)

 * 9 replies
 * 2 participants
 * Last reply from: [jaypabs](https://wordpress.org/support/users/jaypabs/)
 * Last activity: [7 years, 7 months ago](https://wordpress.org/support/topic/flush-w3tc-cache-programmatically-doesnt-work/#post-10770430)
 * Status: not resolved