Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* This file is for loading all mu-plugins within subfolders
* where the PHP file name is exactly like the directory name + .php.
*
* Example: /mu-tools/mu-tools.php
*/
$dirs = glob( dirname( __FILE__ ) . '/*' , GLOB_ONLYDIR );
// Get an array of plugin directories.
$plugins = array_map( function ( string $dir ) : ?array {
if ( ! file_exists( $dir . DIRECTORY_SEPARATOR . basename( $dir ) . ".php" ) ) {
return null;
}
return [
basename( $dir ) => $dir . DIRECTORY_SEPARATOR . basename( $dir ) . '.php',
];
}, $dirs );
// Remove any invalid plugins.
$plugins = array_filter( $plugins, 'is_array' );
// Load the plugins.
foreach ( $plugins as $plugin => $path ) {
require( $path );
}
// Build UI to view the autoloaded must-use plugins.
add_action( 'admin_init', function () use ( $plugins ) {
$rows = [];
foreach ( $plugins as $plugin => $path ) {
$data = get_plugin_data( $path );
$name = $data['Plugin Name'] ?? $plugin;
$desc = $data['Description'] ?? '&nbsp;';
$id = sanitize_title($name);
$rows[] = <<<HTML
<tr id="$id" class="active">
<th scope="row" class="check-column"></th>
<td class="plugin-title"><strong style="padding-left: 10px;">+&nbsp;$name</strong></td>
<td class="column-description desc">
<div class="plugin-description"><p>$desc</p></div>
</td>
</tr>
HTML;
}
// Inject the HTML
add_action('after_plugin_row_mu-autoloader.php', function () use ( $rows ) {
implode( $rows );
} );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.