Conditional to check if Comet Cache active

I am making use of the ability to clear the cache dynamically, as explained here: https://cometcache.com/kb-article/clearing-the-cache-dynamically/

It works well, but I have just discovered that I get a fatal error if I deactivate Comet Cache (e.g. for troubleshooting something else). I think the best way to resolve the issue would be to add a conditional, so that my dynamic function will run only if it detects that Comet Cache is active.

Can anyone suggest what that conditional might be?

@KTS915 You should be able to use is_plugin_active() for this:
https://codex.wordpress.org/Function_Reference/is_plugin_active

Thanks, Raam!

It turns out it’s a bit more complicated than that, but you started me on the road to a solution.

This ended up being my full code:
function check_some_other_plugin() { if ( is_plugin_active( 'comet-cache-pro.php' ) ) { function my_custom_clear_cache() { comet_cache::clear(); } } } add_action( 'admin_init', 'check_some_other_plugin' );
The explanation for its convoluted nature is here: http://wordpress.stackexchange.com/questions/9345/is-plugin-active-function-doesnt-exist

Having fiddled with this further, I can now confirm that this can be simplified to the following, more sensible code:
function my_custom_clear_cache() { if ( is_plugin_active( 'comet-cache-pro.php' ) ) { comet_cache::clear(); } } add_action( 'admin_init', 'my_custom_clear_cache' );
Thanks again, Raam!

1 Like