Skip to content

Instantly share code, notes, and snippets.

@thomaskrakow
Last active September 14, 2022 07:24
Show Gist options
  • Save thomaskrakow/318f84ffdf66040fc51a1a74e894f135 to your computer and use it in GitHub Desktop.
Save thomaskrakow/318f84ffdf66040fc51a1a74e894f135 to your computer and use it in GitHub Desktop.
WordPress Must Use Plugin which activate Contact Form 7 only on contact page.
<?php
/*
Plugin Name: TK-MU-Plugin
Description:
Author: Thomas Krakow
Version: 1.0
Author URI: https://thomas-krakow.de/it/css-und-javascript-nur-bei-bedarf-auf-wordpress-seiten-einfugen/
*/
$fRequestURI = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
if( false === strpos( $fRequestURI, '/wp-admin/' ) ){
add_filter( 'option_active_plugins', 'tk_activate_plugins' );
}
function tk_activate_plugins( $plugins ){
global $fRequestURI;
$is_contact_page = strpos( $fRequestURI, '/kontakt/' );
$is_rest_api = strpos( $fRequestURI, '/contact-forms/' );
$k = array_search( "contact-form-7/wp-contact-form-7.php", $plugins );
if( false !== $k && false === $is_contact_page && false === $is_rest_api ){
unset( $plugins[$k] );
}
return $plugins;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment