Optimize contact form 7 CSS & JS
<?php
// Deregister Contact Form 7 styles
add_filter( 'wpcf7_load_css', '__return_false' );
// Re-register Contact Form 7 styles for specfic pages
function wpcf7_enqueue_styles_new() {
if ( is_page( array( 'contact-us', 'contact' ) ) ) {
wp_enqueue_style( 'contact-form-7-new',
wpcf7_plugin_url( 'includes/css/styles.css' ),
array(), WPCF7_VERSION, 'all' );
//Load RTL CSS contact form7
wp_enqueue_style( 'contact-form-7-rtl-new',
wpcf7_plugin_url( 'includes/css/styles-rtl.css' ),
array(), WPCF7_VERSION, 'all' );
}
}
add_action( 'wp_print_styles', 'wpcf7_enqueue_styles_new', 100 );
// Deregister Contact Form 7 JavaScript
add_action( 'wp_print_scripts', 'contact_form7_deregister_javascript', 100 );
function contact_form7_deregister_javascript() {
if ( ! is_page( array( 'contact-us', 'contact' ) ) ) {
wp_deregister_script( 'contact-form-7' );
}
}
?>