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

James Parsons by James Parsons Updated Feb 2nd, 2024

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.

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

Related Code Entries

Written by James Parsons

James Parsons is the founder and CEO of Content Powered, a premier content marketing agency that leverages nearly two decades of his experience in content marketing to drive business growth. Renowned for founding and scaling multi-million dollar eCommerce businesses through strategic content marketing, James has become a trusted voice in the industry, sharing his insights in Search Engine Watch, Search Engine Journal, Forbes, Entrepreneur, Inc, and other leading publications. His background encompasses key roles across various agencies, contributing to the content strategies of major brands like eBay and Expedia. James's expertise spans SEO, conversion rate optimization, and effective content strategies, making him a pivotal figure in the industry.