Last active
January 20, 2025 12:13
-
-
Save Jany-M/d849c897b835a745de9f to your computer and use it in GitHub Desktop.
[WordPress] Flush Object Cache (Memcached) & Transients - Admin buttons
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 | |
// Flush Cache from Admin bar | |
function flush_memcache_button() { | |
global $wp_admin_bar; | |
// If User isnt even logged in or if admin bar is disabled | |
if ( !is_user_logged_in() || !is_admin_bar_showing() ) | |
return false; | |
// If user doesnt have the perms | |
if ( function_exists('current_user_can') && false == current_user_can('activate_plugins') ) | |
return false; | |
// Button args | |
$wp_admin_bar->add_menu( array( | |
'parent' => '', | |
'id' => 'flush_memcache_button', | |
'title' => __( 'Flush Cache' ), | |
'meta' => array( 'title' => __( 'Delete memcache / WP persistent object cache' )), | |
'href' => wp_nonce_url( admin_url( 'index.php?action=delcachepage'), 'flush_memcache_button' )) | |
); | |
} | |
if ( file_exists( WP_CONTENT_DIR . '/object-cache.php' ) ) { | |
add_action( 'admin_bar_menu', 'flush_memcache_button', 35 ); | |
} | |
function flush_memcache() { | |
global $_wp_using_ext_object_cache; | |
// Check Perms | |
if ( function_exists('current_user_can') && false == current_user_can('activate_plugins') ) | |
return false; | |
// Flush Cache | |
if ( isset( $_GET[ 'action' ] ) && $_GET[ 'action' ] == 'delcachepage' && ( isset( $_GET[ '_wpnonce' ] ) ? wp_verify_nonce( $_REQUEST[ '_wpnonce' ], 'flush_memcache_button' ) : false ) ) { | |
// Adapt this command line to your own memcached | |
error_log(shell_exec("sudo /usr/bin/systemctl restart memcached2")); | |
wp_redirect(admin_url().'?cache_type=object&cache_status=flushed'); | |
die(); | |
} else { | |
wp_redirect(admin_url().'?cache_type=object&cache_status=not_flushed'); | |
die(); | |
} | |
} | |
if ( isset( $_GET[ 'action' ] ) && $_GET[ 'action' ] == 'delcachepage' ) { | |
add_action( 'admin_init', 'flush_memcache'); | |
} | |
// Flush Transients from Admin bar | |
function flush_transients_button() { | |
global $wp_admin_bar; | |
// If User isnt even logged in or if admin bar is disabled | |
if ( !is_user_logged_in() || !is_admin_bar_showing() ) | |
return false; | |
// If user doesnt have the perms | |
if ( function_exists('current_user_can') && false == current_user_can('activate_plugins') ) | |
return false; | |
// Button args | |
$wp_admin_bar->add_menu( array( | |
'parent' => '', | |
'id' => 'flush_transients_button', | |
'title' => __( 'Flush Transients' ), | |
'meta' => array( 'title' => __( 'Delete all WP Transients but not persistent Object Cache (eg. memcached)' )), | |
'href' => wp_nonce_url( admin_url( 'index.php?action=deltransientpage'), 'flush_transients_button' )) | |
); | |
} | |
add_action( 'admin_bar_menu', 'flush_transients_button', 35 ); | |
function flush_transients() { | |
global $_wp_using_ext_object_cache; | |
// Check Perms | |
if ( function_exists('current_user_can') && false == current_user_can('activate_plugins') ) | |
return false; | |
// Flush Cache | |
if ( isset( $_GET[ 'action' ] ) && $_GET[ 'action' ] == 'deltransientpage' && ( isset( $_GET[ '_wpnonce' ] ) ? wp_verify_nonce( $_REQUEST[ '_wpnonce' ], 'flush_transients_button' ) : false ) ) { | |
// Get all Transients | |
global $wpdb; | |
$sql = "SELECT `option_name` AS `name`, `option_value` AS `value` | |
FROM $wpdb->options | |
WHERE `option_name` LIKE '%transient_%' | |
ORDER BY `option_name`"; | |
$get_all_site_transients = $wpdb->get_results( $sql ); | |
// Delete all Transients | |
foreach ($get_all_site_transients as $transient) { | |
$transient_name = str_replace(array('_transient_timeout_', '_transient_', '_site_transient_', '_site_transient_timeout_'), '', $transient->name); | |
delete_transient($transient_name); | |
} | |
// If using object cache | |
if($_wp_using_ext_object_cache) { | |
// DELETE SPECIFIC CUSTOM TRANSIENTS - using a custom array | |
/*foreach ($transients_custom_array as $transients_custom) { | |
wp_cache_delete($transients_custom); | |
}*/ | |
wp_cache_flush(); | |
} | |
wp_redirect(admin_url().'?cache_type=transients&cache_status=flushed'); | |
die(); | |
} else { | |
wp_redirect(admin_url().'?cache_type=transients&cache_status=not_flushed'); | |
die(); | |
} | |
} | |
if ( isset( $_GET[ 'action' ] ) && $_GET[ 'action' ] == 'deltransientpage' ) { | |
add_action( 'admin_init', 'flush_transients'); | |
} | |
function flush_display_admin_msg() { | |
if($_GET[ 'cache_status' ] == '') | |
return; | |
// Display Msg | |
if ( $_GET[ 'cache_status' ] == 'flushed' ) { ?> | |
<div class="updated"> | |
<p><?php echo ucwords($_GET[ 'cache_type' ]); ?> Cache was successfully flushed.</p> | |
</div> | |
<?php | |
} elseif ( $_GET[ 'cache_status' ] == 'not_flushed' ) { ?> | |
<div class="error"> | |
<p><?php echo ucwords($_GET[ 'cache_type' ]); ?> Cache was NOT flushed.</p> | |
</div> | |
<?php | |
} | |
} | |
add_action( 'admin_notices', 'flush_display_admin_msg' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment