Restrict Contact Form 7 CSS and JS To Form Pages

There’s no need to make users load the CSS and JS for Contact Form 7 if they’re not using the Contact Form. This code in your functions php will let you restrict the loading of the CF7 CSS and JS to only the ID specified. Change the number 55 to match the ID of the page you use for your contact form.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* DONT LOAD CONTACT FORM PAGE 7 SITE WIDE */
 
add_filter( 'wpcf7_load_js', '__return_false' );
add_filter( 'wpcf7_load_css', '__return_false' );
 
 
add_action( 'genesis_before_loop', 'cf7_contact_page_only', 1);
function cf7_contact_page_only() {
  if ( is_page( 55 ) ) { 
       if ( function_exists( 'wpcf7_enqueue_scripts' ) ) {
        wpcf7_enqueue_scripts();
    }
    if ( function_exists( 'wpcf7_enqueue_styles' ) ) {
        wpcf7_enqueue_styles();
    } 
  }
}