Support » Plugin: Contact Form 7 » Loading the CF7 CSS and JS only on relevant pages.

  • Resolved morgyface

    (@morgyface)


    I was delighted to find this page which outlines how to prevent the CF7 JS and CSS loading on pages that don’t contain any CF7 forms, however after I added the code to wp-config and then, in turn, added the php to the relevant template, not a lot happened, the CSS/JS was still loading on every page.

    I wonder if this is perhaps a dated post and is no longer relevant?

    Does anyone know how to achieve non-loading of JS/CSS on irrelevant pages?

    https://wordpress.org/plugins/contact-form-7/

Viewing 4 replies - 1 through 4 (of 4 total)
  • I figured it out. I was mistakenly putting the define('WPCF7_LOAD_JS', false); in functions.php instead of wp-config.php. Once I moved it to the config file, it worked as intended.

    Still no luck for me. I’ve added define('WPCF7_LOAD_JS', false); in wp-config and then added the enqueue php in the header of my contact page template (above wp_head) yet it has no effect. All the JS and CSS files load across the site.

    After a few searches thought I’d try adding the following to header.php:

    <?php
    	if ( (!is_page('Contact')) && (function_exists( 'wpcf7_enqueue_scripts' )) ) {
    		wpcf7_dequeue_scripts();
    		wpcf7_dequeue_styles();
    	}
    ?>

    But no joy. No content loads.

    Okay I have sussed it. The position of…

    define('WPCF7_LOAD_JS', false);
    define('WPCF7_LOAD_CSS', false);

    …within wp-config.php is critical. I originally pasted it in at the bottom of the code and it had no effect.

    I moved it to sit directly under define('WPLANG', ''); and it works just fine.

    To be fair to Takayuki Miyoshi he does say on this page that “your lines must be placed before the last line“. I read that as “if I leave comments on the last line and put the code above it will work”. It didn’t.

    Hope this helps someone else.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Loading the CF7 CSS and JS only on relevant pages.’ is closed to new replies.