Created
April 3, 2019 06:54
-
-
Save joseph-farruggio/575cc046af7a1b7f282909b004ec1ef4 to your computer and use it in GitHub Desktop.
Two versions of a script that disables plugins except specific pages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @package active-plugins | |
* @version 1.0 | |
* | |
* Plugin Name: Active Plugins | |
* Plugin URI: http://wordpress.org/extend/plugins/# | |
* Description: This is a development plugin | |
* Author: Carlo Daniele | |
* Version: 1.0 | |
* Author URI: https://carlodaniele.it/ | |
*/ | |
// This is a working version - Works with a single page ($is_contact_page) | |
// Place this file in /wp-content/mu-plugins/active-plugins.php | |
$request_uri = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ); | |
$is_admin = strpos( $request_uri, '/wp-admin/' ); | |
if( false === $is_admin ){ | |
// filter active plugins | |
add_filter( 'option_active_plugins', function( $plugins ){ | |
global $request_uri; | |
$is_contact_page = strpos( $request_uri, '/contact-us/' ); | |
// Plugins to disable later | |
$myplugins = array( | |
"contact-form-7/wp-contact-form-7.php", | |
"contact-form-arkenea/contact_form_arkenea.php" | |
); | |
if( false === $is_contact_page ){ | |
$plugins = array_diff( $plugins, $myplugins ); | |
} | |
return $plugins; | |
} ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @package active-plugins | |
* @version 1.0 | |
* | |
* Plugin Name: Active Plugins | |
* Plugin URI: http://wordpress.org/extend/plugins/# | |
* Description: This is a development plugin | |
* Author: Carlo Daniele | |
* Version: 1.0 | |
* Author URI: https://carlodaniele.it/ | |
*/ | |
// This is NOT a working version - I'd like to add more pages than just /contact-us | |
// Place this file in /wp-content/mu-plugins/active-plugins.php | |
$request_uri = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ); | |
$is_admin = strpos( $request_uri, '/wp-admin/' ); | |
if( false === $is_admin ){ | |
// filter active plugins | |
add_filter( 'option_active_plugins', function( $plugins ){ | |
global $request_uri; | |
// Plugins to disable later | |
$myplugins = array( | |
"contact-form-7/wp-contact-form-7.php", | |
"contact-form-arkenea/contact_form_arkenea.php" | |
); | |
// Pages I'd like to keep $myplugins enabled | |
$pages = array( | |
'/web-app-development/', | |
'/ui-ux-design-agency/', | |
'/healthcare-software-development/', | |
'/roadmapping/', | |
'/contact-us/', | |
); | |
foreach ($pages as $page) { | |
if( false === strpos( $request_uri, $page ) ) { | |
$plugins = array_diff( $plugins, $myplugins ); | |
} else { | |
$containsPlugins = count(array_intersect($myplugins, $plugins)) == count($myplugins); | |
if ( !$containsPlugins ) { | |
array_push($myplugins, $plugins); | |
} | |
return $plugins; | |
} | |
} | |
return $plugins; | |
} ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment