Load jQuery from the footer - by Fabio - on WordPress WooCommerce support

This topic has 9 replies, 2 voices, and was last updated 4 years, 3 months ago ago by Olga Barlow

  • Avatar: fabio_vito
    Fabio
    Participant
    June 7, 2020 at 16:48

    Dear Support,
    I would like to load the google CDN jQuery from the footer (at the moment is loaded from the header).
    I have added the below code in the functions.php however, once I do so the stretch row and content applied to the homepage (e.g. in the banner and the blogs row) does not longer work. It looks like that this functionality gets disabled.

    Could you please help me to understand the reason and fix the problem?

    Alternatively, help me to implement a code in the theme that forces google CDN to be loaded from the footer?

    This is the code that I added:

    add_action('init', 'use_jquery_from_google');
    function use_jquery_from_google () {
    	if (is_admin()) {
    		return;
    	}
    
    	global $wp_scripts;
    	if (isset($wp_scripts->registered['jquery']->ver)) {
    		$ver = $wp_scripts->registered['jquery']->ver;
                    $ver = str_replace("-wp", "", $ver);
    	} else {
    		$ver = '1.12.4';
    	}
    
    	wp_deregister_script('jquery');
    	wp_register_script('jquery', "//ajax.googleapis.com/ajax/libs/jquery/$ver/jquery.min.js", false, $ver);
    }

    Thanks.

    Fabio.

    8 Answers
    Avatar: Olga Barlow
    Olga Barlow
    Support staff
    June 9, 2020 at 10:57

    Hello,

    Unfortunately, scripts of our theme require jQuery to be first before they start to work, that’s why we call it in the head before loading of all the other content to be sure that there won’t be any issues with theme scripts. It can’t be moved to footer, sorry.

    Regards

    Avatar: fabio_vito
    Fabio
    Participant
    June 9, 2020 at 13:01

    Dear Olga,

    Thank you for your reply. To be honest this is a problem because Google is penalizing me because the google CDN jQuery is too slow when loaded from the header.
    I read on interned that it is common practice to load jQueries from the footer and all themes that assure fast loading time, follow this implementation.

    How come Legenda does not allow this, does this mean that it is an obsolete theme, not in line with the necessary requirements from google?
    Could you please investigate if there is any possibility to update Legenda so that it could be in line with advanced themes and fast loading?
    Do you have the same issue also for the theme XStore?

    Thanks for your clarification.
    Fabio.

    Avatar: Olga Barlow
    Olga Barlow
    Support staff
    June 10, 2020 at 13:42

    Hello,

    Try the following code to speed up the loading

    add_action('wp_enqueue_scripts', function() {
    	if ( !is_admin() ) {
    		wp_deregister_script( 'jquery' );
    		wp_register_script( 'jquery', "//ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js", false, '1.12.4' );
    		wp_enqueue_script( 'jquery' );
    	}
    }, 130);

    Let me know if it helps.

    Regards

    Avatar: fabio_vito
    Fabio
    Participant
    June 11, 2020 at 18:37

    Dear Olga,

    Thank you for the suggestion however, the code is very similar to the one I have reported previously. Therefore, unfortunately induces the same issue: the stretch row affect does not work anymore and it is clearly visible on the homepage, for example.

    Is there a way to update the Legenda theme so that it could aligned with google requirements for fast loading?
    Do you have the same issue also for the theme XStore?

    Thanks in advance.

    Fabio.

    Avatar: Olga Barlow
    Olga Barlow
    Support staff
    June 13, 2020 at 07:53

    Hello,

    Take my apologies for the delay in answering and thank you for your patience!
    Could you, please, provide us with WPDashboard and FTP access to site where you test the code to check if we could fix it in any way?

    XStore has an improved JS structure but also requires jQuery before loading other scripts for the correct displaying of the elements during page loading and after that.

    Regards

    Avatar: fabio_vito
    Fabio
    Participant
    June 13, 2020 at 09:27

    Dear Olga,

    No problem, I know that this issue might take more time to be fixed. Please find below the access details.

    I appreciate your help and I hope you could find a solution.

    Please take note of the changes that you make because I would need to replicate the solution on the actual staging that will pushed live (different than the one I provided you).

    Thanks.

    Fabio.

    Please contact administrator
    for this information.
    Avatar: fabio_vito
    Fabio
    Participant
    June 17, 2020 at 18:27

    Dear Olga,

    Could you please give me an update about this issue?

    Thanks.

    Fabio.

    Avatar: Olga Barlow
    Olga Barlow
    Support staff
    June 18, 2020 at 17:57

    Hello,

    Take my apologies for the late reply. Unfortunately, I don’t have good news. To be sure that moving jQuery to footer doesn’t cause any problems requires a lot of recording and we did not find quick solution for this.

    XStore has a better background for this and probably developers will be able to add option to move scripts to the footer for the XStore theme. But Legenda – not sure.

    Regards

  • Viewing 9 results - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.Log in/Sign up

8theme customization service
We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.