Skip to content

Instantly share code, notes, and snippets.

@Firsh
Created June 6, 2017 16:18
Show Gist options
  • Save Firsh/679e28ed5396cb2a369b22926f3effa6 to your computer and use it in GitHub Desktop.
Save Firsh/679e28ed5396cb2a369b22926f3effa6 to your computer and use it in GitHub Desktop.
Disable x plugin when not needed
<?php
// http://wordpress.stackexchange.com/questions/114345/load-plugin-selectively-for-pages-or-posts-etc
// http://codex.wordpress.org/Must_Use_Plugins
add_filter( 'option_active_plugins', 'enable_plugins_selectively' );
function enable_plugins_selectively( $plugins ) {
// Dont' mess with the plugins if it's the admin, yo
//if(is_admin() || stripos($_SERVER["SCRIPT_NAME"], strrchr(wp_login_url(), '/')) !== false){
// return $plugins;
//}
$rules = array(
// page where plugin needed plugin to keep
'/custom-post-types/' => 'wc/woocommerce.php',// OK
'/social-gallery/' => 'social-gallery/SocialGallery.php', // OK
'/product/' => 'wc/woocommerce.php',// OK
'/jetpack-carousel/' => 'carousel-nojetpack/carousel-without-jetpack.php',// OK
'/changelog-update-history/' => 'bootstrap-affix-scrollspy/bootstrap-affix-scrollspy.php'// OK
);
$current_page = add_query_arg( array() );
foreach ($rules as $needle => $plugin_file) {
add_filter('plugin_action_links_'.$plugin_file, 'add_action_links');
if(strpos($current_page,$needle) !== false){
// If current page should load the plugin
$plugins[] = $plugin_file;
}else{
// Plugin should be disabled
$plugin_index = array_search($plugin_file, $plugins);
if($plugin_index !== false){
unset($plugins[$plugin_index]);
}
}
}
return $plugins;
}
function add_action_links( $links) {
array_unshift($links, '<span style="color:red;">MU Force-disabled!</span>');
return $links;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment