• Resolved ivydev

    (@pinkivy)


    Can you please give us an example of how to use WP_Optimize()->get_page_cache()->purge(), as referenced on https://getwpo.com/documentation/ (end of page)? I have tried to call it from the WordPress action plugins_loaded in functions.php, as directed, but the cache never clears. No errors either.

Viewing 7 replies - 1 through 7 (of 7 total)
  • @pinkivy You can use it in your active theme’s functions.php file like this

    add_action("plugins_loaded", "purge_cache");
    
    function purge_cache()
    {
    WP_Optimize()->get_page_cache()->purge();
    }
    • This reply was modified 2 years, 9 months ago by Harshad.
    Thread Starter ivydev

    (@pinkivy)

    Thanks. But I am not seeing that this is working. If I understand correctly, this should be fired once activated plugins have loaded on a page. So, after adding this code, if I browse around to a few pages on the site, the code should have fired a few times, purging the cache. So I refresh WP Optimize in the admin panel, and I look at Current cache size to check that it has gone down, but it always stays the same (or higher).
    Does “plugins_loaded” not fire when I think it does, or this really isn’t working?

    @pinkivy Could you please try to use it with init and check if it works?

    add_action("init", "purge_cache");
    
    function purge_cache()
    {
    WP_Optimize()->get_page_cache()->purge();
    }
    Thread Starter ivydev

    (@pinkivy)

    Thanks, this does fire, but it fires all the time. Is it possible to do it only once a day, something like this? (This does not work as it is.):

    add_action( 'init', 'purge_cache' );
     
    function purge_cache() {
    	/*WP_Optimize()->get_page_cache()->purge();*/
    	if (!wp_next_scheduled('my_purge_cache_hook')) {
    		wp_schedule_event( '1644938400000', 'daily', 'my_purge_cache_hook' ); 
    	}
    }
    
    add_action ( 'my_purge_cache_hook', 'my_purge_cache_function' );
    
    function my_purge_cache_function() {
    	WP_Optimize()->get_page_cache()->purge();
    }

    @pinkivy Does the above code works for you?

    The code looks legit and should work

    Thread Starter ivydev

    (@pinkivy)

    No, it doesn’t work. No matter how much I browse around the site and then check the size of the cache, it never goes down with this code in place.

    I also installed the WP Crontrol plugin, which allows me to see what’s happening in the WP-Cron system, to make sure I scheduled the task correctly. It shows my_purge_cache_function() (which runs WP_Optimize()->get_page_cache()->purge()) in the scheduler, and gives me the option to “Run now”, but when I run it, there is no effect. Even after I browse around the site a little bit after the run, in case it requires page loads to actually run the code. But the cache does not change.

    @pinkivy I think then you’ll have to run it on init action or manually purge the cache from WP-Optimize > Cache

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘WP_Optimize()->get_page_cache()->purge()’ is closed to new replies.