WordPress.org

Forums

Contact Form 7
[resolved] [Broken] Loading JavaScript and Stylesheet Only When it is Necessary (7 posts)

  1. mpdeegan
    Member
    Posted 11 months ago #

    I had previously followed the advice in this post
    http://contactform7.com/loading-javascript-and-stylesheet-only-when-it-is-necessary/ and everything was working just dandy.

    However, I updated to the latest release of the Contact Form 7 plugin and there has been a code changes to contact-form-7/includes/controller.php

    function wpcf7_enqueue_scripts() {
    	if ( ! wpcf7_load_js() ) {    // this test was added
    		return;
    	}
    
    function wpcf7_enqueue_styles() {
    	if ( ! wpcf7_load_css() ) {    // this test was added
    		return;
    	}

    Which means the logic previously applied in wp-config.php to delay loading JavaScript and stylesheets no longer works. If you have the two variables defined in wp-config.php (as described in the link above):

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

    Then even if you modify your template files to manually call the functions wpcf7_enqueue_scripts() and wpcf7_enqueue_styles(), before wp_head() is called, the functions just 'return' without loading the necessary JavaScript and Stylesheet files...

    But maybe I'm missing something here... are other people experiencing the same issue?

    Thanks in advance.

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

  2. Takayuki Miyoshi
    Member
    Plugin Author

    Posted 11 months ago #

    Right. I'm aware of the issue. WPCF7_LOAD_JS and WPCF7_LOAD_CSS do still work (as default settings), but calling wpcf7_enqueue_scripts and wpcf7_enqueue_styles don't work like what is stated in the doc.

    I'll fix it soon. Sorry for the inconvenience.

  3. mpdeegan
    Member
    Posted 11 months ago #

    Hey no problem, thanks for the quick response. I just wasn't sure if you were aware of the issue. Great plugin BTW.

  4. johnja1
    Member
    Posted 11 months ago #

    hey Mpdeegan,

    I'm trying to understand what is the need to stop the JS and CSS from loading ? Will the form load faster and if not loaded how will it get all the config from the CSS and Js to run on the form? I'm just leaning the benefits. Please let me know.

    Thanks
    Jacob

  5. mpdeegan
    Member
    Posted 11 months ago #

    Hey Jacob,

    By default the plugin loads the necessary JavaScript and CSS stylesheet on ALL pages and this work around was to change that default behaviour to just load the files on pages which contain contact forms. It's a performance tweak.

    The plugin author outlined the work around here http://contactform7.com/loading-javascript-and-stylesheet-only-when-it-is-necessary/

    After updating to the latest release, the work around no longer works, but a fix is coming.

    Hope that helps.

  6. john-michael
    Member
    Posted 9 months ago #

    @Takayuki Miyoshi

    Just to be clear, this was fixed, right?

    EDIT: I see it was fixed in 3.9.1. Thanks!

  7. mpdeegan
    Member
    Posted 9 months ago #

    yeah @john-michael its fixed and working for me
    thanks to @Takayuki Miyoshi

Reply

You must log in to post.

About this Plugin

  • Contact Form 7
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic