Skip to content

Instantly share code, notes, and snippets.

@SalvatoreNoschese
Created March 29, 2024 11:29
Show Gist options
  • Save SalvatoreNoschese/4f56f2c2b44fed0d6b9a584088c82bd4 to your computer and use it in GitHub Desktop.
Save SalvatoreNoschese/4f56f2c2b44fed0d6b9a584088c82bd4 to your computer and use it in GitHub Desktop.
Mu-Plugins: Disable Contact Form 7 Assets Except on Contact Page
<?php
/**
* Plugin Name: Disable Contact Form 7 Assets Except on Contact Page
* Description: Disables Contact Form 7 assets (JavaScript, CSS, and reCaptcha) on all pages except the contact page.
* Version: 0.1
* Author: Salvatore Noschese
* Author URI: https://salvatorenoschese.it/
*/
// Function to disable Contact Form 7 assets on all pages except the contact page
add_action( 'wp_enqueue_scripts', 'disable_cf7_assets_except_contact_page' );
function disable_cf7_assets_except_contact_page(){
if ( !is_page('contattami') ) { // Check if it's not the contact page
add_filter( 'wpcf7_load_js', '__return_false' ); // Disable loading CF7 JavaScript
add_filter( 'wpcf7_load_css', '__return_false' ); // Disable loading CF7 CSS
remove_action( 'wp_enqueue_scripts', 'wpcf7_recaptcha_enqueue_scripts', 20 ); // Disable loading CF7 reCaptcha script
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment