Clear home page with API using mu-plugin?

Good Morning,

Now that I have a better understanding how to use Comet Cache, it has really worked out well for us—thanks.

Should an emergency situation on our college campus arise (fire, power outage, active shooter, etc…), I can post a specialized alert header bar to our home page. I can do this via email to post, or of course, on the backend on a laptop or desktop. If by email from a mobile phone, then I need to be able to clear the cache for the home page quickly on a mobile device. I think the best way to do this would be to aggregate your KB posts (https://cometcache.com/kb-article/is-there-an-api-call-that-clears-the-cache/) and (https://cometcache.com/kb-article/can-i-clear-a-specific-url-via-php/). PHP is not my specialty, but would something like this work with a mu-plugin?

<?php add_action('init', function() { if(!empty($_GET['cc_clear']) && $_GET['cc_clear'] === 'my-secret-key') { comet_cache::clearPage(87); exit('cache cleared'); } }); I only want to show the alert bar on the home page and therefore only want to clear the home page. Or, maybe there is a better way to do this? Thanks in advance, Jason

Hi Jason,

Yes, the information in those two articles provides you with everything you’d need to clear the cache for a specific URL (e.g., the home page) by simply visiting a URL.

In Is there an API call that clears the cache? the comet_cache::clear(); function is used to clear the entire cache when visiting the URL, but you could replace that with comet_cache::clearUrl('http://example.com/'); where example.com is your home page address. Or if you want to clear the cache for a specific Post/Page ID (e.g., if you’re using a Static Page as your home page), then you can use comet_cache::clearPage(87);

Once implementing this, I do recommend running some tests to make sure everything is working as expected.

1 Like

Thank you for your reply. I will try this out on our development server next week and report back!

@raamdev, Thanks again for this info but it is not working as it should. Here is the info I have. Any help is appreciated!

mu-plugins > cc-clear.php

<?php /* Plugin Name: Comet Cache Clear Description: Clear the cache of a specific page by entering the page ID. Author: Comet Cache Version: 1.0 */ add_action('init', function() { if(!empty($GET['cclear']) && $GET['cclear'] === '000') { comet_cache::clearPage(87);; exit('cache cleared'); } });

@raamdev, and the code in the page says the following. (FYI, I also have your disable wipe and clear mu-plugin enabled. )

Comet Cache is NOT caching this page, because $_GET contains query string data. The current configuration says NOT to cache GET requests with a query string.

I just added the following MU-Plugin to my test site running the latest free version of Comet Cache:

add_action('init', function()
{
    if(!empty($_GET['cclear']) && $_GET['cclear'] === '000')
     { comet_cache::clear();; exit('cache cleared'); }
});

When I visit http://example.com/?cclear=000 (where example.com is my test site), I see the following message:

cache cleared

When I review the cache directory, I see that all of the cache files have been cleared.


If you’re seeing something different, I’d try temporarily disabling any other custom code you have running (e.g., the other MU-Plugins) and I’d also try using the clear() command for testing, to make sure that much is working (i.e., clear the whole cache). Once you’ve confirmed that’s working, you can change it to clearPage(87) and see if the cache file for Page ID 87 is cleared.

1 Like

Thanks again for your assistance, @raamdev. I see now where the issue is (which is in your initial API tutorial). The URL command is not ?cc_clear=my-secret-key rather it is what you stated ?cclear=my-secret-key. Fix here is to use ?cclear instead of ?cc_clear. Also, clearPage does not seem to be included in your API, however, clearPost worked for me with the page ID. Here is the final code including the additional page to clear!

File located in mu-plugins > cc-clear.php

<?php /* Plugin Name: Comet Cache Clear Description: Clear the cache of a specific page by entering the post ID or URL. Author: Comet Cache Version: 1.0 */ add_action('init', function() { if(!empty($_GET['cclear']) && $_GET['cclear'] === 'my-secret-key') { comet_cache::clearPost(87); comet_cache::clearPost(10604); exit('Page cache successfully cleared, and the current alert notification bar will now be displayed.'); } }); Thank you again, and maybe alter the code in the (https://cometcache.com/kb-article/is-there-an-api-call-that-clears-the-cache/) tutorial?

I only used ?cclear=my-secret-key above because that’s what you had used in your example, so I assumed you wanted the variable to be called cclear. That variable can be anything, as long as it matches what you use in the MU-Plugin. In the KB Article, we recommend using cc_clear, in which case the URL you’d use should end with ?cc_clear=my-secret-key. If you use cclear in your MU-Plugin, then the URL should use cclear.

You are correct that there is no clearPage() API call—I mistakenly followed your lead on that when I reviewed your code example in the first post above. :slight_smile: If you’re clearing a Page or a Post, the API call is clearPost() (in WordPress, Posts and Pages are actually stored in the same place in the database—they’re both technically “Posts”. For that reason, Comet Cache only has a clearPost() API call.) Sorry for the confusion!

1 Like