Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Must-use plugin to filter active plugins in on a per-page basis.
<?php
// returns the path of the request URI without the query string
// see http://php.net/manual/en/function.parse-url.php
// and http://php.net/manual/en/reserved.variables.server.php
// and http://php.net/manual/en/url.constants.php
$request_uri = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
$is_admin = strpos( $request_uri, '/wp-admin/' );
// add filter in front pages only
if( false === $is_admin ){
add_filter( 'option_active_plugins', 'kinsta_option_active_plugins' );
}
/**
* Filters active plugins
*
* @param array $plugins An array of active plugins.
*/
function kinsta_option_active_plugins( $plugins ){
global $request_uri;
$is_contact_page = strpos( $request_uri, '/contact/' );
$unnecessary_plugins = array();
// conditions
// if this is not contact page
// deactivate plugins
if( false === $is_contact_page ){
$unnecessary_plugins[] = 'contact-form-7/wp-contact-form-7.php';
$unnecessary_plugins[] = 'custom-post-type-ui/custom-post-type-ui.php';
$unnecessary_plugins[] = 'query-monitor/query-monitor.php';
}
foreach ( $unnecessary_plugins as $plugin ) {
$k = array_search( $plugin, $plugins );
if( false !== $k ){
unset( $plugins[$k] );
}
}
return $plugins;
}
@aelsharawi

This comment has been minimized.

Show comment
Hide comment
@aelsharawi

aelsharawi May 7, 2017

it did not work with me (looks like another caching plugin is interfering, I have w3t, varnish)
also, it removes the word "contact" if it is in the main menu and when typing the url contact it redirects to main page

aelsharawi commented May 7, 2017

it did not work with me (looks like another caching plugin is interfering, I have w3t, varnish)
also, it removes the word "contact" if it is in the main menu and when typing the url contact it redirects to main page

@aelsharawi

This comment has been minimized.

Show comment
Hide comment

aelsharawi commented May 7, 2017

@carlodaniele

This comment has been minimized.

Show comment
Hide comment
@carlodaniele

carlodaniele Jun 2, 2017

Consider that this plugin is just an example supporting a tutorial. It's not meant to be used in production. If needed, the developer should carefully test the plugin on his own installation before putting it at work in production. The plugin has been successfully tested on several installations, but it could not be tested under every conditions.

Owner

carlodaniele commented Jun 2, 2017

Consider that this plugin is just an example supporting a tutorial. It's not meant to be used in production. If needed, the developer should carefully test the plugin on his own installation before putting it at work in production. The plugin has been successfully tested on several installations, but it could not be tested under every conditions.

@luiginica

This comment has been minimized.

Show comment
Hide comment
@luiginica

luiginica Jan 6, 2018

This one is for plugins. https://wordpress.org/support/plugin/wp-asset-clean-up/ is for css/js. If you for need css/js you can also use gonzales.
@carlodaniele do you know an alternative plugin like yours? Thank you!

luiginica commented Jan 6, 2018

This one is for plugins. https://wordpress.org/support/plugin/wp-asset-clean-up/ is for css/js. If you for need css/js you can also use gonzales.
@carlodaniele do you know an alternative plugin like yours? Thank you!

@shirokoweb

This comment has been minimized.

Show comment
Hide comment
@shirokoweb

shirokoweb May 22, 2018

This won't work, you can't send emails (CF7) breaks the plugin

shirokoweb commented May 22, 2018

This won't work, you can't send emails (CF7) breaks the plugin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment