Fix for Filesystem not writeable error in Redis Object Cache

Redis Object Cache by Till Kruss - screenshot of a plugin logo in the WordPress repository

Some of the WordPress users who use Redis Object Cache plugin (by Till Krüss) on their websites could experience a “File System: Not Writeable” error from this plugin. This could be caused by the use of DISALLOW_FILE_MODS constant in the WordPress configuration file and therefore some little adjustments must me made.

Redis Object Cache showing Filesystem: Not Writeable error
Redis Object Cache plugin – showing a “Filesystem: Not writeable” error

DISALLOW_FILE_MODS – Better leave it enabled

Many WordPress developers who use version control (e.g. GIT) for their websites certainly do not want any file modifications on a live version of their website. This is both – for security and data integrity, therefore it is logical that they have set this constant to “true” in the WordPress configuration file.

However changing DISALLOW_FILE_MODS constant to FALSE is not a correct solution to this problem. Leave it on if you use it, because there is another fix we can use – specially for Redis.

Why Redis Object Cache do not work with the DISALLOW_FILE_MODS being on anymore?

Starting from the version 2.5.0, the Redis Object Cache plugin now “respects” the use of DISALLOW_FILE_MODS and therefore informs the WordPress admin that the file system is not writeable. While the plugin is still working, after disabling it – it will not allow to enable it again until you solve the issue.

Fix for the problem with DISALLOW_FILE_MODS constant being on

Just place this code in your theme functions.php file or add it as a must-use plugin:

/*B:Allow file modifications for  certain operations*/ 
add_filter( 'file_mod_allowed', 'dwp_allow_file_mod_for_certain_ops', 10, 2 );
function dwp_allow_file_mod_for_certain_ops( $allow_file_mod, $context ) {
  if ( in_array($context, ['object_cache_dropin'])) {
    return true;
  } else {
    return $allow_file_mod;
  }
}
/*E:Allow file modifications for certain operations*/ 

And this should solve the issue with file system check.

About the author
I'm a full-stack WordPress developer with a 10+ years of solid experience in the core web development languages, development processes / techniques, web security, Linux server management and with pretty good understanding about proper semantics, UX/UI, technical SEO, good design and basic knowledge of company leadership. On top of that - a distant 5+ years experience as a computer and electronics repair technician which often enables me to understand also how the things work at the hardware level.