• Resolved bhogben

    (@bhogben)


    Hi all,

    I’ve done some google-fu but can’t find any solutions that work so sorry if there is already an answer!

    I’m doing some speed optimisation on a clients site and they want to use this plugin.

    I’ve noticed that when jquery is deregistered via

    wp_deregister_script('jquery');

    and reregistered via

    wp_register_script('jquery_2', 'https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js', '', '', true);
    wp_enqueue_script( 'jquery_2' );
    

    then scripts.js is not loaded, which is fine, as expected – jquery is a dependency

    But when enqueuing the below, ajax form submission doesn’t work? Does anyone know a way around?

    wp_register_script( 'cf7', plugins_url('contact-form-7/includes/js/scripts.js'), array('jquery_2'), '', true );
    wp_enqueue_script('cf7');
    

    I’ve also tried via js:

    function asyncLoad(url) {
      d = document;
      var script = d.createElement('script'),
        s = d.scripts[0];
      script.src = url;
      script.async = true;
      s.parentNode.insertBefore(script, s);
    }
    asyncLoad('/wp-content/plugins/contact-form-7/includes/js/scripts.js');
    
    function initContactForm() {
      $('div.wpcf7 > form').each(function() {
        var $form = $(this);
        wpcf7.initForm($form);
        if (wpcf7.cached) {
          wpcf7.refill($form);
        }
      });
    }
    
    jQuery(document).ready(function($) {
    initContactForm();
    });

    but this produces the error wpcf7 is not defined

    I know the simple solution is “queue jquery” – but the render blocking aspect of jquery being included in the header does actually make a pretty huge difference on mobile – it’s the difference between 3.5 seconds load and under 3 seconds on 3g networks

    • This topic was modified 6 years, 9 months ago by bhogben.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter bhogben

    (@bhogben)

    An update, I got this to work by editing contact form 7 files – which is not ideal!

    Could I get a response from the devs on a future fix or option to custom enqueue jquery?

    This represents a substantial performance improvement possibility

    in includes/controller.php

    	//if ( 'header' === wpcf7_load_js() ) {
    	//	$in_footer = false;
    	//}
    
    	wp_enqueue_script( 'contact-form-7',
    		wpcf7_plugin_url( 'includes/js/scripts.js' ),
    		array( 'jquery_2' ), WPCF7_VERSION, $in_footer );
    Thread Starter bhogben

    (@bhogben)

    Marking issue as resolved.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘wpcf7 is not defined when jquery is unenqueued’ is closed to new replies.