Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@thefrosty
Last active February 7, 2017 02:07
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 thefrosty/8303566 to your computer and use it in GitHub Desktop.
Save thefrosty/8303566 to your computer and use it in GitHub Desktop.
Enables the loading of plugins not sitting in the mu-plugins directory to allow auto-updates and correct directory reads.
<?php
/**
* Plugin Name: Autoload Plugins
* Plugin URI: https://gist.github.com/thefrosty/8303566
* Description: Autoload non-MU plugins that live in the `wp-content/plugins` directory. This allows auto-updates to
* work but have to plugin act like an must-use plugin.
* Version: 0.2.1
* Author: Austin Passy
* Author URI: http://austin.passy.co/
*/
namespace TheFrosty;
use WP_Plugins_List_Table;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* These are default plugin that live in the `wp-content/plugins` directory that you want autoloaded.
* Add any plugins you wish to the $plugins array or filter in your own using the
* 'mu_autoload_plugins_array' filter. Be sure to call that filter as early as possible.
*
* @return array
*/
function autoload_plugins() {
$plugins = array();
$plugins[] = 'dev-debug-bar/debug-bar.php';
$plugins[] = 'dev-debug-bar-actions-and-filters-addon/debug-bar-action-and-filters-addon.php';
$plugins[] = 'query-monitor/query-monitor.php';
apply_filters( 'mu_autoload_plugins_array', $plugins );
foreach ( $plugins as $plugin_file ) {
if ( ! file_exists( trailingslashit( WP_PLUGIN_DIR ) . $plugin_file ) ) {
unset( $plugins[ $plugin_file ] );
}
}
return $plugins;
}
add_filter( 'all_plugins', function( $plugins ) {
foreach ( autoload_plugins() as $plugin_file ) {
if ( isset( $plugins[ $plugin_file ] ) ) {
unset( $plugins[ $plugin_file ] );
}
}
return $plugins;
} );
add_action( 'muplugins_loaded', function() {
$default_hook = current_action(); // muplugins_loaded
$action_hook = (string) apply_filters( 'mu_plugins_autoload_action_hook', $default_hook );
add_action( $action_hook, function() {
// `get_plugins` is not included by default
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
foreach ( autoload_plugins() as $plugin_file ) {
if ( file_exists( trailingslashit( WP_PLUGIN_DIR ) . $plugin_file ) ) {
if ( is_plugin_active( $plugin_file ) || is_plugin_active_for_network( $plugin_file ) ) {
deactivate_plugins( $plugin_file );
}
require trailingslashit( WP_PLUGIN_DIR ) . $plugin_file;
}
}
}, 8 );
}, 5 );
/**
* Add rows for each plugin under this plugin when listing mu-plugins in wp-admin
*/
add_action( 'admin_init', function() {
add_action( 'after_plugin_row_' . basename( __FILE__ ), function() {
$table = new WP_Plugins_List_Table;
foreach ( autoload_plugins() as $plugin_file ) {
$data = get_plugin_data( trailingslashit( WP_PLUGIN_DIR ) . $plugin_file, false );
$name = empty( $data[ 'Name' ] ) ? $plugin_file : $data[ 'Name' ];
$data[ 'Name' ] = "&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;&nbsp;" . $name;
$table->single_row( array( $plugin_file, $data ) );
}
} );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment