Created
August 23, 2020 13:12
-
-
Save finalwebsites/87cb64fb348ffd7994e4227efcf891bc to your computer and use it in GitHub Desktop.
Dequeue cf7 JavaScript and reCaptcha scripts and preventing them from loading everywhere.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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