wpcf7 is not defined when jquery is unenqueued
-
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
- The topic ‘wpcf7 is not defined when jquery is unenqueued’ is closed to new replies.