AI Verified

Name

Purge Wordpress

About

Adds a button at the top of Wordpress Admin Top bar

Language

PHP

Rating

Voted: 0 by 0 user(s)

Codevault

WebSquadron

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.1

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

History

Last modified:

27/05/2023

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

Found 0 vulnerabilities

Purge Wordpress

 
                    
1/*
2Plugin Name: Purge Cache
3Description: Adds a button to the WordPress dashboard to clear the object cache
4*/
5 
6add_action( 'admin_bar_menu', 'add_purge_cache_button', 999 );
7 
8function add_purge_cache_button( $wp_admin_bar ) {
9 if ( ! current_user_can( 'manage_options' ) ) {
10 return;
11 }
12 
13 $args = array(
14 'id' => 'purge-cache',
15 'title' => 'Purge Cache',
16 'href' => '#',
17 'meta' => array( 'class' => 'purge-cache' )
18 );
19 $wp_admin_bar->add_node( $args );
20}
21 
22add_action( 'admin_footer', 'add_purge_cache_script' );
23 
24function add_purge_cache_script() {
25 if ( ! current_user_can( 'manage_options' ) ) {
26 return;
27 }
28 ?>
29 <script>
30 jQuery(document).ready(function($) {
31 $('#wp-admin-bar-purge-cache').click(function() {
32 if (confirm('Are you sure you want to purge the cache?')) {
33 $.ajax({
34 url: '<?php echo admin_url( 'admin-ajax.php' ); ?>',
35 data: {
36 action: 'purge_cache',
37 },
38 success: function() {
39 alert('Cache purged successfully!');
40 },
41 error: function() {
42 alert('An error occurred while purging the cache.');
43 }
44 });
45 }
46 });
47 });
48 </script>
49 <?php
50}
51 
52add_action( 'wp_ajax_purge_cache', 'purge_cache_callback' );
53 
54function purge_cache_callback() {
55 global $wp_object_cache;
56 if ( ! current_user_can( 'manage_options' ) ) {
57 wp_die();
58 }
59 
60 wp_cache_flush();
61 
62 wp_die();
63}

0

Related Snippets

Please see some snippets below related to this snippet..

Performance

AI Verified

4

Ensure Webfont is Loaded (Elementor)

Added: 1 year ago

Last Updated: 3 months ago

Ensure Webfonts are loaded

Performance

AI Verified

1

Explicit Fixed Width and Height that Excludes Elementor Carousel

Added: 11 months ago

Last Updated: 11 months ago

This is the code to use when the original snippet to fix this issue affected Elementor Carousel.

Performance

AI Verified

0

Add Custom Image Sizes

Added: 1 year ago

Last Updated: 1 year ago

This snippet allows you to define custom image sizes that can be used throughout your WordPress site.

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

Performance

AI Verified

32

Remove Unused Javascript

Added: 1 year ago

Last Updated: 3 days ago

Remove Unused Javascript - and improve your Page Speed Insight Score

WooCommerce

Pro Verified

10

Deactivate some WooCommerce Checkout Fields

Added: 1 year ago

Last Updated: 2 weeks ago

Deactivate some WooCommerce Checkout Fields from showing

Elementor

AI Verified

6

CSS Grid Aid

Added: 8 months ago

Last Updated: 1 month ago

This can be used for any WordPress Builder to aid working with CSS Grids.