Created
February 18, 2015 20:35
-
-
Save tomazzaman/ffb39ea623870591a358 to your computer and use it in GitHub Desktop.
Clear WP_Object_Cache on WPEngine to get around ACF's updating bug.
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 | |
// Register script and add necessary options to it, make sure the path is correct | |
function flush_redis_enqueue_scripts() { | |
wp_enqueue_script( 'flush-redis', plugin_dir_url( __FILE__ ) . 'js/script.js', array( 'jquery' ) ); | |
wp_localize_script( 'flush-redis', 'FlushRedis', array( | |
'ajaxUrl' => admin_url( 'admin-ajax.php' ), | |
'flushRedisNonce' => wp_create_nonce( 'flush-redis-nonce' ), | |
) | |
); | |
} | |
add_action( 'admin_enqueue_scripts', 'flush_redis_enqueue_scripts' ); | |
// Add the Flush Redis button in the top admin bar | |
function add_redis_flush_to_admin($admin_bar) { | |
$admin_bar->add_menu( array( | |
'id' => 'redis-flush', | |
'parent' => false, | |
'title' => 'Flush Redis', | |
'href' => '#', | |
'meta' => array( | |
'class' => 'flush-redis', | |
'title' => __('Flush Redis'), | |
), | |
) ); | |
} | |
add_action('admin_bar_menu', 'add_redis_flush_to_admin', 100); | |
// Clear object cache, register function as AJAX | |
function flush_redis() { | |
global $wp_object_cache; | |
$nonce = $_POST['nonce']; | |
if ( ! wp_verify_nonce( $nonce, 'flush-redis-nonce' ) ) { | |
die ( 'Busted!' ); | |
} | |
echo $wp_object_cache->flush(); | |
exit; | |
} | |
add_action( 'wp_ajax_flush_redis', 'flush_redis' ); | |
// Add a Redis flushed notice only when redis-flushed GET parameter is set | |
function flush_redis_notice() { | |
if ( isset( $_GET['redis-flushed'] ) && $_GET['redis-flushed'] == 1 ) { | |
echo '<div class="updated"><p><strong>Redis flushed.</strong></p></div>'; | |
} | |
} | |
add_action( 'admin_notices', 'flush_redis_notice' ); |
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
(function($) { | |
$(document).ready(function() { | |
$('.flush-redis').click(function(event) { | |
$.ajax({ | |
type : "post", | |
url : FlushRedis.ajaxUrl, | |
data : {action: "flush_redis", nonce: FlushRedis.flushRedisNonce}, | |
success: function(response) { | |
var separator = (document.URL.indexOf('?') === -1) ? "?" : "&"; | |
window.location = document.URL + separator + "redis-flushed=" + response | |
} | |
}); | |
event.preventDefault(); | |
}); | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment