I am new in wordpress technology i apologies if i ask basic question here.
actually i am trying to remove contact form 7 style and js file from all other pages except contact us, so what i did in function.php
add_action( 'wp_enqueue_scripts','pine_scripts' );
function pine_scripts() {
if(! is_page(22) )
{
wp_dequeue_script('contact-form-7'); // Dequeue JS Script file.
wp_dequeue_style('contact-form-7'); // Dequeue CSS file.
}
}
but i still see in source code of every page contact form 7 is being included
/* <![CDATA[ */
var wp_load_style = {"0":"admin-bar.css","1":"jquery.prettyphoto.css","2":"video-lightbox.css","3":"wp-block-library.css","4":"page-visit-counter-pro.css","6":"controlled-admin-access.css","7":"dashicons.css","8":"post-views-counter-frontend.css","9":"wordfenceAJAXcss.css","10":"quadro-theme-style.css","11":"magnific-popup.css","12":"cherry-projects-styles.css","13":"cherry-google-fonts-quadro.css","14":"duplicate-post.css","15":"tablepress-default.css","16":"yoast-seo-adminbar.css","17":"popup-maker-site.css","18":"contact-form-7.css","19":"tm-builder-swiper.css","20":"tm-builder-modules-style.css","21":"font-awesome.css","22":"current-template-style.css","23":"wp-add-custom-css.css"};
var wp_load_script = ["admin-bar.js","jquery.js","jquery.prettyphoto.js","video-lightbox.js","cherry-js-core.js","tm-builder-modules-global-functions-script.js","page-visit-counter-pro.js","controlled-admin-access.js","wordfenceAJAXjs.js","smush-lazy-load.js","quadro-theme-script.js","magnific-popup.js","cherry-projects-single-scripts.js","cherry-post-formats.js","google-recaptcha.js","popup-maker-site.js","contact-form-7.js","google-maps-api.js","divi-fitvids.js","waypoints.js","tm-jquery-touch-mobile.js","tm-builder-frontend-closest-descendent.js","tm-builder-frontend-reverse.js","tm-builder-frontend-simple-carousel.js","tm-builder-frontend-simple-slider.js","tm-builder-frontend-easy-pie-chart.js","tm-builder-frontend-tm-hash.js","tm-builder-modules-script.js","tm-builder-swiper.js","fittext.js"];
var cherry_ajax = "0901ce207c";
var ui_init_object = {"auto_init":"false","targets":[]};
/* ]]> */
My website url: https://www.mobitsolutions.com/home
and contact us url: https://www.mobitsolutions.com/contactus
any help would be appreciated.
4
Answers
You can use this plugin :
https://wordpress.org/plugins/scripts-removal-for-contact-form-7/
Or you can do :
This solution checks for the shortcode and only enqueues the scripts and styles on pages where the shortcode is used.
As of November 2020, an additional tag seems required to effectively dequeue the recaptcha script: "wpcf7-recaptcha" and "google-recaptcha" in combination.
Be careful: dequeuing cf7/recaptcha3 script it is not recommended because recaptcha 3 needs to examine user patterns on site to properly do its job:
https://contactform7.com/faq-about-recaptcha-v3/#stop-script-loading