Skip to content

Instantly share code, notes, and snippets.

@Digiover
Last active November 23, 2016 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Digiover/6d3985617e04135881892b1c0467dad6 to your computer and use it in GitHub Desktop.
Save Digiover/6d3985617e04135881892b1c0467dad6 to your computer and use it in GitHub Desktop.
Make WordPress writable before, and read-only after, WordPress updates with this MU-Plugin
<?php
/**
* Plugin Name: Make Read Only &amp; Clear Read Only WordPress
* Plugin URI: https://www.saotn.org
* Description: Makes all WordPress files read-only after updates, and writable just before updates. This should ease WordPress updates when it's read-only on the file system. Please donate <a rel="nofollow" target="_blank" href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&amp;hosted_button_id=J24FGGU96YSUY" title="donate to Sysadmins of the North in Dollars">$2.50 USD</a> (or <a rel="nofollow" target="_blank" href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&amp;hosted_button_id=QLX4RPPL3L8LY" title="donate to Sysadmins of the North in Euros">&euro; 2,50 Euro</a>) through PayPal to support me in my research time and hosting costs.
* Network: True
* Version: 1.0
* Author: Jan Reilink
* Author URI: https://www.saotn.org
* License: GPLv2
*/
// change your path here:
$path = "d:/www/example.com/www";
function make_readonly($obj) {
$chunks = explode('/', $obj);
chmod($obj, '0600');
}
function make_writable($obj) {
$chunks = explode('/', $obj);
chmod($obj, '0755');
}
function pre_install_writeable($dir) {
if($objs = glob($dir."/*")) {
foreach($objs as $obj) {
make_writable($obj);
if(is_dir($obj)) pre_install_writeable($obj);
}
}
error_log($dir ." made writable");
return make_writable($dir);
}
function post_install_readonly($dir) {
if($objs = glob($dir."/*")) {
foreach($objs as $obj) {
make_readonly($obj);
if(is_dir($obj)) post_install_readonly($obj);
}
}
error_log($dir ." made readonly");
return make_readonly($dir);
}
function _prefix_post_install_readonly() {
global $path;
post_install_readonly($path);
}
function _prefix_pre_install_writeable() {
global $path;
pre_install_writeable($path);
}
add_filter('upgrader_pre_install', '_prefix_pre_install_writeable', 10, 2);
add_filter('upgrader_post_install', '_prefix_post_install_readonly', 10, 2);
?>
@Digiover
Copy link
Author

Digiover commented Nov 23, 2016

DEMO code to make an already read-only WordPress site temporarily writable for WordPress Core-, Theme- and Plugin updates. Afterwards the WordPress site is made read-only again. Fun upgrader_pre_install() and upgrader_post_install() usage.

This code is tested only once to prove a point for myself and coworkers. Use this at your own risk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment