Contact Form 7 Loads JavaScript on ALL Pages
Contact Form 7 is one of the most popular form plugins available for WordPress. It’s great for making simple contact forms. Most Contact Form 7 users go with it over a more advanced plugin like Gravity Forms because they only need a simple form for their viewers to email them. Thanks to its FREE price tag, it’s often the number-one choice when this is all that’s needed.
One annoying thing about Contact Form 7 is that it loads its JavaScript on every page! This causes increased page loading times and can, therefore, impact search rankings. There’s no reason for the CF7 JS to be loaded on any page(s) other than the pages that contain a form. This code snippet blocks the Contact Form 7 JavaScript from loading on pages other than your form pages (i.e. Contact).
//* Load Contact Form 7 JavaScript on Specific Pages add_action( 'wp_print_scripts', 'deregister_cf7_javascript', 100 ); function deregister_cf7_javascript() { if ( ! is_page( 25 ) ) //Replace 25 with your contact page's ID wp_deregister_script( 'contact-form-7' ); }
Hemratna Bhimani says
For Stylesheet (CSS)
add_action( ‘wp_print_styles’, ‘deregister_cf7_styles’, 100 );
function deregister_cf7_styles() {
if ( ! ( is_page( 20 ) || is_page( 1699 ) ) ) {
wp_deregister_style( ‘contact-form-7’ );
}
}
Hemratna Bhimani says
Hi Christoph,
Thank you for your help.
The javascript part is working fine. 🙂
But the same thing for CSS is not working.
I try to add below code in the same function.
wp_deregister_style( ‘contact-form-7-css’ );
wp_dequeue_style(‘contact-form-7-css’);
Please help me to remove CSS also.
Thank you.