Created
July 27, 2022 10:06
-
-
Save iniznet/dd38b188eefb85a2ef5cd74fa6a89b9a to your computer and use it in GitHub Desktop.
WordPress: Automatically copied requirements folder to mu-plugin on theme activation
This file contains hidden or 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
add_action('after_switch_theme', function() { | |
$basePath = dirname(__DIR__, 3); | |
$muPluginpath = dirname(__DIR__, 2) . '/mu-plugins'; | |
$requirementsPath = __DIR__ . '/requirements'; | |
require_once( $basePath . "/wp-admin/includes/plugin.php"); | |
if (wp_mkdir_p($muPluginpath)) { | |
$di = new RecursiveDirectoryIterator($muPluginpath, FilesystemIterator::SKIP_DOTS); | |
$ri = new RecursiveIteratorIterator($di, RecursiveIteratorIterator::CHILD_FIRST); | |
foreach ( $ri as $file ) { | |
$file->isDir() ? rmdir($file) : unlink($file); | |
} | |
} | |
// extract all zip files in requirementsPath to muPluginPath | |
$di = new RecursiveDirectoryIterator($requirementsPath, FilesystemIterator::SKIP_DOTS); | |
$ri = new RecursiveIteratorIterator($di, RecursiveIteratorIterator::CHILD_FIRST); | |
foreach ( $ri as $file ) { | |
if ($file->isFile() && $file->getExtension() === 'zip') { | |
$zip = new ZipArchive(); | |
$res = $zip->open($file); | |
if ($res === true) { | |
$zip->extractTo($muPluginpath); | |
$zip->close(); | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment