AI Verified

Name

Link to clear Bunny CDN

About

I wanted a way for my clients to have a one click "Clear Bunny CDN" without having to go into other settings. Worked with AI and BunnyCDN support on this.

Language

PHP

Rating

Voted: 1 by 1 user(s)

How to Setup Snippet

Just add it as an admin snippet with your API from the Bunny dashboard and your Pull Zone ID which you can find as a numeric value in the URL of the specific CDN page for that site (where you clear it manually on Bunny CDN)

Codevault

lowthian

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

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.

Website/ Profile URL:

https://www.lowthiandesign.com

History

Last modified:

01/03/2024

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

Link to clear Bunny CDN

 
                    
1// Add a custom link to the admin bar for clearing Bunny CDN cache
2function wpb_custom_toolbar_link($wp_admin_bar) {
3 $args = array(
4 'id' => 'bunny-clear-cache',
5 'title' => 'Clear Bunny CDN Cache',
6 'href' => '#',
7 'meta' => array(
8 'class' => 'bunny-clear-cache',
9 'title' => 'Clear Bunny CDN Cache',
10 'onclick' => 'clearBunnyCDNCache(); return false;' // Ensure onclick is properly set for calling the JS function
11 )
12 );
13 $wp_admin_bar->add_node($args);
14}
15add_action('admin_bar_menu', 'wpb_custom_toolbar_link', 999);
16 
17// Inject JavaScript for handling the clear cache action in the admin footer
18function bunny_cdn_clear_cache_script() {
19 ?>
20 <script type="text/javascript">
21 function clearBunnyCDNCache() {
22 if (!confirm('Are you sure you want to clear the Bunny CDN cache?')) return;
23 fetch('<?php echo admin_url('admin-ajax.php'); ?>', {
24 method: 'POST',
25 credentials: 'same-origin',
26 headers: {
27 'Content-Type': 'application/x-www-form-urlencoded',
28 },
29 body: 'action=clear_bunny_cdn_cache'
30 })
31 .then(response => response.json())
32 .then(data => {
33 if (data.success) {
34 alert(data.data.message);
35 } else {
36 alert('Failed to clear cache. ' + data.data.message);
37 }
38 })
39 .catch(error => console.error('Error:', error));
40 }
41 </script>
42 <?php
43}
44add_action('admin_footer', 'bunny_cdn_clear_cache_script');
45 
46// Handle the AJAX request to clear the Bunny CDN cache
47function wp_ajax_clear_bunny_cdn_cache() {
48 $apiKey = 'YOUR_API_KEY'; // Replace with your actual Bunny.net API key
49 $pullZoneId = 'YOUR_ZONE_ID'; // Replace with your actual Pull Zone ID.
50 $apiUrl = "https://api.bunny.net/pullzone/{$pullZoneId}/purgeCache";
51 
52 $response = wp_remote_post($apiUrl, array(
53 'method' => 'POST',
54 'headers' => array(
55 'Content-Type' => 'application/json',
56 'AccessKey' => $apiKey,
57 ),
58 'body' => '{}',
59 'data_format' => 'body',
60 ));
61 
62 if (is_wp_error($response)) {
63 $error_message = $response->get_error_message();
64 wp_send_json_error(['message' => "WP Error: $error_message"]);
65 } else {
66 $status = wp_remote_retrieve_response_code($response);
67 if ($status === 204) { // HTTP 204 No Content, indicates success with no return data
68 wp_send_json_success(['message' => 'Cache cleared successfully.']);
69 } else if ($status !== 200) { // Any other status code not equal to 200 is treated as an error
70 $body = wp_remote_retrieve_body($response);
71 wp_send_json_error(['message' => "API Error: HTTP $status - $body"]);
72 } else {
73 wp_send_json_success(['message' => 'Cache cleared successfully.']);
74 }
75 }
76}
77add_action('wp_ajax_clear_bunny_cdn_cache', 'wp_ajax_clear_bunny_cdn_cache');

1

Related Snippets

Please see some snippets below related to this snippet..

WordPress Admin

AI Verified

0

Disable Gutenberg Editor and back to Classic Widgets

Added: 1 year ago

Last Updated: 1 year ago

Disable Gutenberg Editor and back to Classic Widgets

WordPress Admin

AI Verified

21

Change admin footer text

Added: 1 year ago

Last Updated: 2 weeks ago

Customises the footer text in the WordPress dashboard.

WordPress Admin

AI Verified

1

Disable Wordpress Admin Bar Test

Added: 1 year ago

Last Updated: 3 months ago

There must be many of these - this is just my test I also want to see if line breaks are preserved in the description   should be free

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

WordPress Admin

AI Verified

1

View update info on posts

Added: 3 weeks ago

Last Updated: 3 weeks ago

WordPress Admin

AI Verified

1

Link to clear Bunny CDN

Added: 4 months ago

Last Updated: 4 months ago

I wanted a way for my clients to have a one click "Clear Bunny CDN" without having to go into other settings. Worked with AI and BunnyCDN support on this.

General

AI Verified

0

Gravity to Stripe Description

Added: 8 months ago

Last Updated: 8 months ago