Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active October 11, 2025 23:42
Show Gist options
  • Save westonruter/9c791f4f8cc1cc37e7b3f4bc2db9be97 to your computer and use it in GitHub Desktop.
Save westonruter/9c791f4f8cc1cc37e7b3f4bc2db9be97 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: Active Plugins Override
* Description: Install this as an mu-plugin to override the active plugins via query parameters.
* Plugin URI: https://gist.github.com/westonruter/9c791f4f8cc1cc37e7b3f4bc2db9be97
* Requires PHP: 7.2
* Version: 0.1.0
* Author: WordPress Performance Team
* Author URI: https://make.wordpress.org/performance/
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Update URI: false
* GitHub Plugin URI: https://gist.github.com/westonruter/9c791f4f8cc1cc37e7b3f4bc2db9be97
* Primary Branch: main
*/
namespace ActivePluginsOverride;
if ( defined( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' ) ) {
return;
}
/**
* Gets the always-active plugins.
*
* @return array
*/
function get_always_active_plugins(): array {
if ( defined( 'ALWAYS_ACTIVE_PLUGINS' ) ) {
if ( is_string( ALWAYS_ACTIVE_PLUGINS ) ) {
return explode( ',', ALWAYS_ACTIVE_PLUGINS );
} elseif ( is_array( ALWAYS_ACTIVE_PLUGINS ) ) {
return ALWAYS_ACTIVE_PLUGINS;
}
}
return array();
}
/**
* Gets a plugin slug.
*
* @param string $plugin_file Plugin file.
* @return string Plugin slug.
*/
function get_plugin_slug( string $plugin_file ): string {
$plugin_file = strtok( $plugin_file, '/' );
return basename( $plugin_file, '.php' );
}
/**
* Gets requested plugins.
*
* @param string[] $query_vars Query vars.
* @return string[] Plugin slugs.
*/
function get_requested_plugins( array $query_vars ): array {
$plugins = array();
foreach ( $query_vars as $key ) {
if ( isset( $_GET[ $key ] ) ) {
if ( is_array( $_GET[ $key ] ) ) {
$plugins = array_merge( $plugins, array_filter( $_GET[ $key ], 'is_string' ) );
} else if ( is_string( $_GET[ $key ] ) ) {
$plugins = array_merge( $plugins, explode( ',', $_GET[ $key ] ) );
}
}
}
if ( count( $plugins ) > 0 ) {
$plugins = array_map(
static function ( string $plugin ): string {
return get_plugin_slug( sanitize_file_name( $plugin ) );
},
$plugins
);
}
return $plugins;
}
/**
* Gets active override plugins.
*
* @return string[] Plugins.
*/
function get_active_override_plugins(): array {
return get_requested_plugins( array( 'active_plugins', 'enable_plugins', 'enabled_plugins' ) );
}
/**
* Gets inactive override plugins.
*
* @return string[] Plugins.
*/
function get_inactive_override_plugins(): array {
return get_requested_plugins( array( 'inactive_plugins', 'disable_plugins', 'disabled_plugins' ) );
}
add_filter(
'option_active_plugins',
static function ( $plugins ) {
if ( ! is_array( $plugins ) ) {
$plugins = array();
}
$active_override_plugins = get_active_override_plugins();
$inactive_override_plugins = get_inactive_override_plugins();
if ( count( $active_override_plugins ) > 0 && count( $inactive_override_plugins ) > 0 ) {
wp_die( 'You cannot supply both active and inactive plugins.', '', array( 'response' => 400 ) );
}
if ( count( $inactive_override_plugins ) > 0 ) {
$plugins = array_filter(
$plugins,
static function ( $plugin ) use ( $inactive_override_plugins ): bool {
return ! in_array( get_plugin_slug( $plugin ), $inactive_override_plugins, true );
}
);
}
if ( count( $active_override_plugins ) > 0 ) {
// TODO: Parse out the "Requires Plugins" header from the force-enabled plugins.
if ( count( array_intersect( $active_override_plugins, array( 'embed-optimizer', 'image-prioritizer' ) ) ) > 0 ) {
$active_override_plugins[] = 'optimization-detective';
$active_override_plugins[] = 'od-admin-ui';
}
$plugins = array();
require_once ABSPATH . '/wp-admin/includes/plugin.php';
foreach ( array_keys( get_plugins() ) as $installed_plugin_file ) {
if ( in_array( get_plugin_slug( $installed_plugin_file ), $active_override_plugins, true ) ) {
$plugins[] = $installed_plugin_file;
}
}
}
if (
isset( $_GET['disable_all_plugins'] ) ||
in_array( 'none', $active_override_plugins, true )
) {
$plugins = array();
}
return array_unique( array_merge( $plugins, get_always_active_plugins() ) );
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment