Skip to content

Instantly share code, notes, and snippets.

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 chrisvanpatten/9840d1b854fd9e2dc4d7d445ff2500a1 to your computer and use it in GitHub Desktop.
Save chrisvanpatten/9840d1b854fd9e2dc4d7d445ff2500a1 to your computer and use it in GitHub Desktop.
<?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