AI Verified

Name

Purge The Site

Language

PHP

Rating

Voted: 0 by 0 user(s)

Codevault

Sisterhood-Travels-Vault

Scroll down to see more snippets from this codevault.

Wordpress Compatability

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

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:

18/11/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 The Site

 
                    
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..

General

AI Verified

0

Cerrar sesion - cliente

Added: 5 months ago

Last Updated: 5 months ago

General

AI Verified

19

Convert To WebP

Added: 7 months ago

Last Updated: 3 days ago

<p>Snippet to convert JPG / PNG / Gif to WebP automatically on upload. Used GD or ImageMagick</p>

General

Unverified

0

Highlight-Text

Added: 1 month ago

Last Updated: 1 month ago

Highlight Text within a Text Editor or a Heading

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

General

AI Verified

1

Ensure webfont is loaded

Added: 7 months ago

Last Updated: 6 months ago

General

Unverified

0

email universal code

Added: 7 months ago

Last Updated: 7 months ago

constant contact email universal code

General

Unverified

0

Google Tag Manager Body

Added: 7 months ago

Last Updated: 7 months ago