Skip to content

Instantly share code, notes, and snippets.

@finalwebsites
Created August 23, 2020 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save finalwebsites/87cb64fb348ffd7994e4227efcf891bc to your computer and use it in GitHub Desktop.
Save finalwebsites/87cb64fb348ffd7994e4227efcf891bc to your computer and use it in GitHub Desktop.
Dequeue cf7 JavaScript and reCaptcha scripts and preventing them from loading everywhere.
<?php
function my_register_cf7_js() {
// Dequeue cf7 and recaptcha scripts, preventing them from loading everywhere
add_filter( 'wpcf7_load_js', '__return_false' ); // Might as well use their filter
wp_dequeue_script( 'google-recaptcha' );
// If current post has cf7 shortcode, enqueue!
global $post;
if ( isset( $post->post_content ) AND has_shortcode( $post->post_content, 'contact-form-7' ) ) {
if ( function_exists( 'wpcf7_enqueue_scripts' ) ) {
wpcf7_enqueue_scripts();
wp_enqueue_script( 'google-recaptcha' );
}
}
}
add_action( 'wp_enqueue_scripts', 'my_register_cf7_js' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment