Skip to content

Instantly share code, notes, and snippets.

@walterebert
Created August 29, 2018 17:24
Show Gist options
  • Save walterebert/7ae9902cb8f139e4c45997a4715aca3e to your computer and use it in GitHub Desktop.
Save walterebert/7ae9902cb8f139e4c45997a4715aca3e to your computer and use it in GitHub Desktop.
WordPress: Only load JS + CSS if a Contact Form 7 form is used
<?php
function customize_cf7_dequeue() {
wp_dequeue_script( 'contact-form-7' );
wp_dequeue_style( 'contact-form-7' );
}
add_action( 'wp_enqueue_scripts', 'customize_cf7_dequeue' );
function customize_cf7_enqueue( $content ) {
if ( has_shortcode( $content, 'contact-form-7' ) ) {
wpcf7_enqueue_styles();
wpcf7_enqueue_scripts();
}
return $content;
}
add_filter( 'the_content', 'customize_cf7_enqueue' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment