Skip to content

Instantly share code, notes, and snippets.

@ali7ali
Last active October 21, 2023 22:08
Show Gist options
  • Save ali7ali/5a6b49ee3c300af3d0c75761514db062 to your computer and use it in GitHub Desktop.
Save ali7ali/5a6b49ee3c300af3d0c75761514db062 to your computer and use it in GitHub Desktop.
WordPress Directories & Files Permissions reset
<?php // phpcs:ignore -- \r\n notice.
/**
* Plugin Name: WordPress Directories & Files Permissions reset
* Description: Correct WordPress directories/files permissions.
* Author: Ali Ali
* Version: 1.0
* Author URI: https://github.com/ali7ali
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
function correct_permissions_recursively($path, $filemode, $dirmode)
{
if (is_dir($path)) {
if (!chmod($path, $dirmode)) {
return;
}
$dh = opendir($path);
while (($file = readdir($dh)) !== false) {
if ($file != '.' && $file != '..') { // skip self and parent pointing directories
$fullpath = $path . '/' . $file;
correct_permissions_recursively($fullpath, $filemode, $dirmode);
}
}
closedir($dh);
} else {
if (is_link($path)) {
return;
}
if (!chmod($path, $filemode)) {
return;
}
}
}
if (!defined('ABSPATH')) {
correct_permissions_recursively(__DIR__ . '/', 0644, 0755);
} else {
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php';
if (function_exists('get_home_path')) {
correct_permissions_recursively(get_home_path(), 0644, 0755);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment