Must-use plugins autoloader: How to use get_plugins() correctly?

‘get_plugins’ is intended to be used only with regular plugins, and also it looks at plugin headers, and return only plugins that have valid one, like

/* Plugin Name: A plugin */

However mu-plugins can work even without that headers.

Also consider that with WP 3.9 was introduced the function wp_register_plugin_realpath that should be used to ensure compatibility with symbolink linked folders.

According to a post of Ryan McCue on make.wordpress.org a sinple, but working mu plugins loader should be something like this:

<?php
$plugins = array(
    'my-mu-plugin/my-mu-plugin.php'
);
foreach ( $plugins as $plugin ) {
    $path = dirname( __FILE__ ) . "https://wordpress.stackexchange.com/" . $plugin;
    // Add this line to ensure mu-plugins subdirectories can be symlinked
    wp_register_plugin_realpath( $path );
    include $path;
}

The $plugins array here is hardcoded. Sincerly I found this approach more functional than the glob + get_plugins

because:

  • it’s faster
  • it works for mu plugins that does not have any plugins header

Sure it need to manually add a line on the array every time you install a new plugin, but imho this is not a great issue, and also gives ability to easily deactivate a mu plugin.

Leave a Comment