Add a "Flush Cloudflare Cache" Button to Your WordPress Admin

Updated 02/02/2024, Posted 02/02/2024 by James Parsons James Parsons 0 Comments

Note: This is only for users of the WP Rocket caching plugin.

This is a handy one. When purging cache, most plugins add the ability to do so right at the top of your admin. For instance, in WP Rocket, you hover your mouse up and click "Purge Cache".

But, if you have the Cloudflare integration, you have to dig deep in the settings to clear it:

  • Settings
    • WP Rocket
      • Add-ons
        • Modify Options
          • Clear all Cloudflare cache files

This is a much easier way. Add this to your functions.php file in your WordPress theme:

/*
 * Add "Flush Cloudflare Cache" button to top wp-admin bar
 */
 
function add_purge_cloudflare_to_admin_bar($wp_admin_bar) {
        if (!is_admin_bar_showing()) {
                return;
        }
        if (!current_user_can('rocket_purge_cloudflare_cache')) {
                return;
        }
        $purge_url = wp_nonce_url(
                admin_url('admin-post.php?action=rocket_purge_cloudflare'),
                'rocket_purge_cloudflare'
        );
        $args = array(
                'id' => 'purge_cloudflare',
                'title' => 'Flush Cloudflare Cache',
                'href' => $purge_url,
                'meta' => array('class' => 'cloudflare-purge')
        );
        $wp_admin_bar - > add_node($args);
}
add_action('admin_bar_menu', 'add_purge_cloudflare_to_admin_bar', 100);

Now you have a handy button up next to your other cache plugins.

Flush Cache

The "Flush PHP OPcache" plugin can be found here.

Related Code Snippets

Written by James Parsons

Hi, I'm James Parsons! I founded Content Powered, a content marketing agency where I partner with businesses to help them grow through strategic content. With nearly twenty years of SEO and content marketing experience, I've had the joy of helping companies connect with their audiences in meaningful ways. I started my journey by building and growing several successful eCommerce companies solely through content marketing, and I love to share what I've learned along the way. You'll find my thoughts and insights in publications like Search Engine Watch, Search Engine Journal, Forbes, Entrepreneur, and Inc, among others. I've been fortunate to work with wonderful clients ranging from growing businesses to Fortune 500 companies like eBay and Expedia, and helping them shape their content strategies. My focus is on creating optimized content that resonates and converts. I'd love to connect – the best way to contact me is by scheduling a call or by email.