Skip to content

Instantly share code, notes, and snippets.

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 dannyconnolly/9811743 to your computer and use it in GitHub Desktop.
Save dannyconnolly/9811743 to your computer and use it in GitHub Desktop.
Only load contact form 7 scripts when needed in wordpress
/**
* Only load contact form 7 script when needed
*/
function cf7_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', 'cf7_dequeue_scripts', 99 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment