• Resolved patrickkaufmann

    (@patrickkaufmann)


    I have included contact form only into a few pages of my website essayhilfe.de, but Contact form 7 loads js and css files at all pages:

    <script type='text/javascript' src='https://essayhilfe.de/app/plugins/contact-form-7/includes/js/jquery.form.min.js?ver=3.51.0-2014.06.20'></script>
    https://essayhilfe.de/app/plugins/contact-form-7/includes/css/styles.css?ver=4.7
    https://essayhilfe.de/app/plugins/contact-form-7/includes/js/scripts.js?ver=4.7
    

    How could I load this files only at specific pages?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Takayuki Miyoshi

    (@takayukister)

    Towhid

    (@cryptex_vinci)

    Hey @patrickkaufmann

    Follow these steps:

    Step 1 – (Identify the page ID where your contact form is visible)

    1. Go to Dashboard > Pages
    2. Click on the page where you are displaying your Contact Form 7 form.
    3. Copy the page ID (124) from the address bar. It will look like this:
      your-site.com/wp-admin/post.php?post=124&action=edit

    Step 2 – De-queue Contact Form 7 CSS & JS from all pages except where contact form is visible

    Insert this in your functions.php

    add_action( 'wp_enqueue_scripts', 'custom_contact_script_conditional_loading' );
    
    function custom_contact_script_conditional_loading(){
       //  Edit page IDs here
       if(! is_page(124) )    
       {		
          wp_dequeue_script('contact-form-7'); // Dequeue JS Script file.
          wp_dequeue_style('contact-form-7');  // Dequeue CSS file. 
       }
    }

    Thanks

    • This reply was modified 7 years, 2 months ago by Towhid.
    • This reply was modified 7 years, 2 months ago by Towhid.
    Ramzii

    (@ramzii)

    Thank you @asif !

    Thanks for the very useful answer.
    I wanted to know when the pages are two with two IDs, how does the function turn?
    I was able to create the function only for the “contact me” page.
    * Stop loading the JavaScript and CSS stylesheet on all pages.
    */
    function wps_contact_form_7_dequeue_scripts() {
    add_filter( ‘wpcf7_load_js’, ‘__return_false’ );
    add_filter( ‘wpcf7_load_css’, ‘__return_false’ );
    }
    add_action( ‘wp_enqueue_scripts’, ‘wps_contact_form_7_dequeue_scripts’ );
    /**
    * Enqueue Contact Form 7 scripts
    *
    * Start loading the JavaScript and CSS stylesheet on certain pages.
    */
    function wps_contact_form_7_enqueue_scripts() {
    // Page ID, title, slug, or array of such.
    if ( is_page( ‘contattami’ ) ) {
    if ( function_exists( ‘wpcf7_enqueue_styles’ ) ) {
    wpcf7_enqueue_styles();
    }
    if ( function_exists( ‘wpcf7_enqueue_scripts’ ) ) {
    wpcf7_enqueue_scripts();
    }
    }
    }
    add_action( ‘wp_enqueue_scripts’, ‘wps_contact_form_7_enqueue_scripts’ );
    To add another page, how do I change the function?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Contact form 7 loads js and css at all pages’ is closed to new replies.