Last active
June 2, 2024 20:00
-
-
Save alfredo-wpmudev/1e7f3439d2d6c0b01f8603de9eb086e8 to your computer and use it in GitHub Desktop.
Clear Static Server Cache and Object Cache(Only site Administrators)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: WPMUDEV Hosting Clear Static Server Cache and Object Cache | |
* Description: Adds a button to the WordPress Admin Bar to clear WPMUDEV Hosting Static Server Cache and Object Cache. | |
* Version: 1.0 | |
* Author: Alfredo Galano Loyola | WPMUDEV | |
* Author URI: https://wpmudev.com/ | |
*/ | |
function cwpai_add_clear_static_cache_button() { | |
global $wp_admin_bar; | |
$blog_id = get_current_blog_id(); | |
if( current_user_can('edit_posts') && is_main_site()){ | |
$wp_admin_bar->add_menu(array( | |
'id' => 'clear_static_cache', | |
'title' => 'Clear Server Cache', | |
'href' => '#', | |
'meta' => array( | |
'title' => __('Clear Server Cache'), | |
), | |
)); | |
} | |
} | |
add_action('admin_bar_menu', 'cwpai_add_clear_static_cache_button', 100); | |
function cwpai_clear_static_cache_button_script() { | |
?> | |
<script type="text/javascript"> | |
jQuery(document).ready(function($) { | |
$('#wp-admin-bar-clear_static_cache').click(function() { | |
$.ajax({ | |
url: '<?php echo admin_url('admin-ajax.php'); ?>', | |
type: 'POST', | |
data: { | |
action: 'cwpai_clear_static_cache' | |
}, | |
success: function(data) { | |
alert(data); | |
window.location.reload(true); | |
} | |
}); | |
}); | |
}); | |
</script> | |
<?php | |
} | |
add_action('admin_footer', 'cwpai_clear_static_cache_button_script'); | |
function cwpai_clear_static_cache() { | |
if (function_exists('wpmudev_hosting_purge_static_cache')) { | |
//Clear Static Server Cache | |
wpmudev_hosting_purge_static_cache(); | |
//Clear Object Cache | |
if( current_user_can('administrator') ){ | |
wp_cache_flush(); | |
echo 'Static Server Cache and Object Cache were cleared.'; | |
}else{ | |
echo 'Static Server Cache was cleared.'; | |
} | |
} else { | |
echo "This site is not hosted with WPMUDEV."; | |
} | |
die(); | |
} | |
add_action('wp_ajax_cwpai_clear_static_cache', 'cwpai_clear_static_cache'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment