Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active February 22, 2020 06:04
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save damiencarbery/e99513c236fee6d7b9a7130652cc6620 to your computer and use it in GitHub Desktop.
Save damiencarbery/e99513c236fee6d7b9a7130652cc6620 to your computer and use it in GitHub Desktop.
Disable Contact Form 7 plugin except for certain pages.
<?php
/*
Plugin Name: Disable CF7 plugin
Plugin URI: http://www.damiencarbery.com
Description: Disable Contact Form 7 plugin except for certain pages.
Author: Damien Carbery
Version: $Revision: $
$Id: $
*/
add_filter( 'option_active_plugins', 'disable_plugins_per_page' );
function disable_plugins_per_page( $plugin_list ) {
// Quit immediately if in admin area.
if ( is_admin() ) {
return $plugin_list;
}
$disable_plugins = array (
// Plugin Name => Urls *not* to disable on.
'contact-form-7/wp-contact-form-7.php' => array( '/wp-json/contact-form-7/', '/contact-us/', '/contact-us-fr/', '/kontakt/' ), // /wp-json url needed to send messages
'advanced-custom-fields-pro/acf.php' => array( '/page-one/', '/page-two/', '/page-three/' ),
);
$plugins_to_disable = array();
foreach ( $plugin_list as $plugin ) {
if (true == array_key_exists( $plugin, $disable_plugins ) ) {
//error_log( "Found $plugin in list of active plugins." );
if ( false === array_search( $_SERVER['REQUEST_URI'], $disable_plugins[ $plugin ] ) ) {
//error_log( "Disable $plugin on ".$_SERVER['REQUEST_URI']."." );
$plugins_to_disable[] = $plugin;
}
}
}
// If there are plugins to disable then remove them from the list,
// otherwise return the original list.
if ( count( $plugins_to_disable ) ) {
$new_list = array_diff( $plugin_list, $plugins_to_disable );
return $new_list;
}
else {
return $plugin_list;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment