Skip to content

Instantly share code, notes, and snippets.

@code-flow
Created May 11, 2023 15:02
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 code-flow/90ce0592d8a27de8aa613bb08db433a9 to your computer and use it in GitHub Desktop.
Save code-flow/90ce0592d8a27de8aa613bb08db433a9 to your computer and use it in GitHub Desktop.
Deactivate WordPress Plugins in the frontend if they're not needed
<?php
/*
Plugin Name: Plugin-Deactivate-Test
Version: 1.0.0
*/
// Place this file in wp-content/mu-plugins/
add_filter( 'option_active_plugins', 'mu_deactivate_unnecessary_plugins' );
function mu_deactivate_unnecessary_plugins( $plugin_list ) {
if ( ! isset( $_SERVER['REQUEST_URI'] ) ) {
return $plugin_list;
}
$current_url = esc_url_raw( $_SERVER['REQUEST_URI'] );
if ( str_contains( $current_url, '/wp-admin' ) || str_contains( $current_url, '/wp-login.php' ) ) {
return $plugin_list;
}
if ( str_contains( $current_url, '/' . rest_get_url_prefix() ) ) {
return $plugin_list;
}
// ... (some other rules)
unset( $plugin_list[2], $plugin_list[3], $plugin_list[4] ); // <-- deactivate a bunch of plugin of your choice for testing purposes
return $plugin_list;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment