phpwordpresscontact-form-7deferred-loading

Defer contact form 7 script & css from other pages


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.


Solution

  • You can use this plugin :

    https://wordpress.org/plugins/scripts-removal-for-contact-form-7/

    Or you can do :

    function rjs_lwp_contactform_css_js() {
        global $post;
       if(! is_page(22) ) {
            wp_enqueue_script('contact-form-7');
             wp_enqueue_style('contact-form-7');
    
        }else{
            wp_dequeue_script( 'contact-form-7' );
            wp_dequeue_style( 'contact-form-7' );
        }
    }
    add_action( 'wp_enqueue_scripts', 'rjs_lwp_contactform_css_js');