Skip to content

Instantly share code, notes, and snippets.

@davidvandenbor
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save davidvandenbor/6a839a560c162b82631c to your computer and use it in GitHub Desktop.

Select an option

Save davidvandenbor/6a839a560c162b82631c to your computer and use it in GitHub Desktop.
WordPress: Prevent Contact 7 javascript and stylesheets from loading on EVERY page
<?php
/**
* Prevent Contact Form 7 javascript and styles from loading on every page
* Load them only on pages that contain the CF7 shortcode!
* @author @david <david@davidvandenbor.nl>
*/
function david_dequeue_scripts() {
$load_scripts = false;
if( is_singular() ) {
$post = get_post();
if( has_shortcode($post->post_content, 'contact-form-7') ) {
$load_scripts = true;
}
}
if( ! $load_scripts ) {
wp_dequeue_script( 'contact-form-7' );
wp_dequeue_style( 'contact-form-7' );
}
}
add_action( 'wp_enqueue_scripts', 'david_dequeue_scripts', 99 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment