Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
# Check: https://onexa.nl/wordpress/toolbar-link-redis-object-cache/
/**
* Add a link to the Admin Toolbar to easily flush the Redis cache (Redis Object Cache plugin)
*
* @author Hiranthi Herlaar, onexa.nl
* @version 2.0
*
* @var $wp_admin_bar > https://codex.wordpress.org/Class_Reference/WP_Admin_Bar
**/
if ( ! function_exists( 'redis_add_toolbar_link') )
{
function redis_add_toolbar_link( $wp_admin_bar )
{
if ( current_user_can( 'manage_options' ) && is_plugin_active( 'redis-cache/redis-cache.php' ) )
{
# Redis Object Cache < 2.x || => 2.x
$RedisObjectCache = ( class_exists( 'RedisObjectCache' ) ) ? new RedisObjectCache : redis_object_cache();
if ( $RedisObjectCache->get_redis_status() )
{
$RedisPage = is_multisite() ? 'settings.php?page=redis-cache' : 'options-general.php?page=redis-cache';
// Flush Redis Cache trough WP Rocket Admin Bar Menu
$args = array(
'id' => 'flush-redis-cache',
'title' => __( 'Flush Redis Cache' ) . '',
'parent' => false,
'href' => wp_nonce_url( network_admin_url( add_query_arg( 'action', 'flush-cache', $RedisPage ) ), 'flush-cache' )
);
$wp_admin_bar->add_node( $args );
} // end REDIS
}
} // end add_toolbar_link
}
add_action( 'admin_bar_menu', 'redis_add_toolbar_link', 999 );
@eksiscloud

This comment has been minimized.

Copy link

@eksiscloud eksiscloud commented Jul 14, 2020

This breaks 2.x updates Redis Cache. I got a tip to add this before redis_add_toolbar_link function.

if ( ! isset( $GLOBALS[ 'redisObjectCache' ] ) ) {
$GLOBALS[ 'redisObjectCache' ] = Rhubarb\RedisCache\Plugin::instance();
}

if ( ! class_exists( 'RedisObjectCache' ) ) :

class RedisObjectCache {
public function get_redis_status() {
return Rhubarb\RedisCache\Plugin::instance()->get_redis_status();
}
}

endif;

@hiranthi

This comment has been minimized.

Copy link
Owner Author

@hiranthi hiranthi commented Jul 15, 2020

@eksiscloud thanks for letting me know the code didn't work with 2.x anymore!

I've updated the code to be compatible with 2.0.x (and it's still compatible with 1.x as well) :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.