Sysadmins of the North
Share now!

Automatically flush Redis cache after publishing a WordPress post

In a previous post I explained that clearing PHP opcode caches before WordPress Updates helps in streamlining the update process. WordPress updates no longer fail because of cached file locations. Did you know you can automatically flush opcode caches like Redis when you publishing a post or page in WordPress? Doing so ensures you and your visitors see the newly created content immediately.

You can use the following PHP script as a WordPress Must Use Plugin. It’ll “fire” on the publish_post action, deleting all cache in Redis whenever you publish a post. Herein lies it’s disadvantage: all caches are flushed, and not just that of the home page for instance.

Important notes follows below the code.

Flush Redis cache after post publish in WordPress
Save the following PHP code as a plugin in a new file called, for example, flush-redis.php and upload the file to your wp-content/mu-pluginsfolder.

 * Plugin Name: Flush Redis cache
 * Plugin URI:
 * Donate URI:
 * Description: Flushes PHP Redis cache upon publish_post action. By clearing the opcode cache in memory you immediately can see a new or changed post.
 * Network: True
 * Version: 1.2
 * Author: Jan Reilink
 * Author URI:
 * License: GPLv2

require_once( ABSPATH .'/wp-config.php' );
function manual_clear_redis_cache() {
	if( class_exists( 'Redis' ) ) {
		$r = new Redis();
		if ($r->connect( WP_REDIS_PATH, 0 )) {
			if( false === $r->flushAll() ) {
				if( WP_DEBUG === true ) {
					error_log( 'After post publish: Flushing PHP Redis failed.' );
				return false;
			else {
				if( WP_DEBUG === true ) {
					error_log( 'After post publish: PHP Redis flushed succesfully.' );
				return true;
		else {
			if( WP_DEBUG === true ) {
				error_log( 'Could not connect to PHP Redis.' );
add_action( 'publish_post', 'manual_clear_redis_cache', 10, 2 );

// frc = flush redis cache
add_filter( 'plugin_row_meta', 'frc_plugin_row_meta', 10, 2 );
function frc_plugin_row_meta($links, $file) {
	if ( !preg_match('/flush-redis.php$/', $file ) ) {
		return $links;

	$links[] = sprintf(
		'<a target="_blank" href="" title="Donate to Jan Reilink / Sysadmins of the North">%s</a>',
		__( 'Donate' )
	return $links;

Important notes and caveats:

  1. this must use plugin to flush Redis cache works perfectly in my situation, it may not in yours.
  2. the plugin relies on a defined value WP_REDIS_PATH, in my case a Unix file-socket. Other Redis opcode cache plugins, like Redis Object Cache, require these connections parameters in wp-config.php, I found it easy to reuse it. If you’re not using a plugin like Redis Object Cache, then add the connection parameters to your wp-config.php anyway, or you may need to change the connect() function.
  3. all cached data in Redis is deleted using a flushAll()function.
  4. success and failure are logged to WP_DEBUG_LOG, if WP_DEBUG is enabled. Otherwise the plugin fails or succeeds silently.
  5. other plugins may or may not have different functionality to flush Redis caches.
This may interest you:   WordPress Crayon Syntax Highlighter Plugin "wp_load" Remote File Inclusion Vulnerability

Please let me know what you think of this little cache busting plugin. If you’re using wp-redis by Pantheon, they offer you two alternative cache flushing solutions on their Wiki.

buy me a coffee
Buy Me A Coffee

About the Author Jan Reilink

My name is Jan. I am not a hacker, coder, developer, programmer or guru. I am merely a system administrator, doing my daily thing at Vevida in the Netherlands. With over 15 years of experience, my specialties include Windows Server, IIS, Linux (CentOS, Debian), security, PHP, WordPress, websites & optimization. Want to support me and donate? Use this link:

follow me on:

Thank you!

Leave a Comment:

Add Your Reply
Skip to content