UMAR-MOBITSOLUTIONS
UMAR-MOBITSOLUTIONS

Reputation: 77984

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.

Upvotes: 1

Views: 4015

Answers (4)

guidobras
guidobras

Reputation: 141

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

Upvotes: 0

Jeremiah Deasey
Jeremiah Deasey

Reputation: 39

As of November 2020, an additional tag seems required to effectively dequeue the recaptcha script: "wpcf7-recaptcha" and "google-recaptcha" in combination.

function remove_contact_scripts() {     
    
    /* remove Contact Form scripts if not viewing Contact page */
    if ( !is_page( array( 'contact-us' ) ) ){
        wp_dequeue_script( 'contact-form-7' );
        wp_dequeue_style( 'contact-form-7' );

        /* these are both needed */
        wp_dequeue_script( 'wpcf7-recaptcha' );
        wp_dequeue_script('google-recaptcha');
    }
} 
add_action('wp_enqueue_scripts', 'remove_contact_scripts', 99);

Upvotes: 1

Howard E
Howard E

Reputation: 5639

This solution checks for the shortcode and only enqueues the scripts and styles on pages where the shortcode is used.

function dd_wpcf7_dequeue_scripts() {

    $load_scripts = false;

    if( is_singular() ) {
        $post = get_post();

        if( has_shortcode($post->post_content, 'contact-form-7') ) {
            $load_scripts = true;

        }

    }

    if( ! $load_scripts ) {
        wp_dequeue_script( 'contact-form-7' );
        wp_dequeue_script('google-recaptcha');
        wp_dequeue_style( 'contact-form-7' );
    }
}
add_action( 'wp_enqueue_scripts', 'dd_wpcf7_dequeue_scripts', 99 );

Upvotes: 2

Jonathan Delean
Jonathan Delean

Reputation: 1084

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');

Upvotes: 0

Related Questions