Last active
August 25, 2022 13:15
-
-
Save developer-anuragsingh/66da315bbc0cb52d3af507aaaa05986f to your computer and use it in GitHub Desktop.
Remove Contact Form 7 & Google reCaptcha Scripts and Stylesheet if short-code not found in post content to improve page speed.
This file contains hidden or 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
/** | |
* Remove Google recaptcha and contact form 7 recaptcha scripts & stylesheets | |
* if shortcode not found in post-content | |
*/ | |
add_action('wp_enqueue_scripts', function () { | |
global $post; | |
if ( is_a( $post, 'WP_Post' ) && !has_shortcode( $post->post_content, 'contact-form-7') ) { | |
// if shortcode not found | |
wp_dequeue_script( 'google-recaptcha' ); | |
wp_dequeue_script( 'wpcf7-recaptcha' ); | |
wp_dequeue_script( 'contact-form-7' ); | |
wp_dequeue_style( 'contact-form-7' ); | |
} else { | |
// if shortcode found | |
wp_enqueue_script( 'google-recaptcha' ); | |
wp_enqueue_script( 'wpcf7-recaptcha' ); | |
wp_enqueue_script('contact-form-7'); | |
wp_enqueue_style('contact-form-7'); | |
} | |
}, 99); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment