Skip to content

Instantly share code, notes, and snippets.

@FutureMedia
Last active May 11, 2021 11:53
Show Gist options
  • Save FutureMedia/b798e2515f7856a59f6caacd046ea84c to your computer and use it in GitHub Desktop.
Save FutureMedia/b798e2515f7856a59f6caacd046ea84c to your computer and use it in GitHub Desktop.
Contact Form 7 (CF7) - load JS & CSS only when it is necessary
<?php
/**
* CF 7 - version 5.4.1
* Load JS & CSS only when necessary
*
* Use WP functions '__return_true', '__return_false' to load the
* necessary files. The 'is_page()' should be called after the init
* hook and the js+css must be enqueued before the wp_head hook.
*
*/
add_filter( 'wpcf7_load_js', '__return_false' );
add_filter( 'wpcf7_load_css', '__return_false' );
function fm_cf7_activate_scripts() {
if (is_page('contact') || is_page('45') || is_singular( array( 'cd', 'book' ) )) {
add_filter( 'wpcf7_load_js', '__return_true' );
add_filter( 'wpcf7_load_css', '__return_true' );
}
}
add_action( 'get_header', 'fm_cf7_activate_scripts' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment