Hi Jan, yes I did, this tim kaye guy went on my nerves, so i left.
You are polite so here I am, for you. - NOT FOR “TIM KAYE” TO DISTURB AGAIN! GET A LIFE! No wonder, websharks’ Jason, Cristian, and Ram disappeared as well!
Jan, Jquery loading in footer is totally fine and smart on all pages where jquery isn’t needed by some plugin or wp function earlier. It costs time to look in console for each plugin’s needs, but ultimately we got there:
- we heavily use s2m: when someone visits a members-only page they are redirected to the s2m membership options page, so WE have to add s2m shortcode to exception list, others may not need to
- below is the code I use in functions.php that decides when jquery gets loaded
- note also that initially I did try with newer jquery libraries (all from google cdn) but for US some older plugins didn’t play well then
- so, testing is key when you “tune” wordpress sites for load speed
- we ended up making the site the fastest wordpress site at the time, as per tools like webpagetest.org, so what we do absolutely works well
- before anyone starts arguing again: fastest not for a cleverly chosen page or homepage, but for the largest page, loading most assets (speed for that was some 479ms or so, of course + server TTFB).
- this is what counts IMHO; anyone can turbo-prop an empty homepage…
- code posting here doesn’t come up correctly, if you need contact me by email for all code
- In our s2hacks mu-plugins file we exclude all plugins that are only used on certain URLs anyway. That code has some private stuff but per email i would share it with you too. Generally it looks like so:
$request_uri = $_SERVER['REQUEST_URI'];
$is_admin = strpos($request_uri,'wp-admin');
// add filter in front pages only
if(false === $is_admin){add_filter('option_active_plugins','remove_unnecessary_plugins');}
function remove_unnecessary_plugins($plugins){
global $request_uri;
$uses_contact_form_7 = strpos($request_uri,'submit-an-entry') || strpos($request_uri,'...
etc.
- Then in functions.php we exclude those plugins that can’t be managed globally by url, so then by shortcode:
//Remove or Restrict scripts and styles to load only when needed
function remove_ticks(){
global $post;
if (!is_admin()){
wp_deregister_script('jquery');
//give special treatment to online health assessment, discussion=true, and shortcodes sociallocker, s2, show-quiz; for others load jquery in footer deferred
if ((false === strpos($_SERVER['REQUEST_URI'],'german-shepherd-dog-health-assessment-online')) && (false === strpos($_SERVER['REQUEST_URI'],'discussion=true')) && (!has_shortcode($post->post_content,'s2')) && (!has_shortcode($post->post_content,'sociallocker')) && (!has_shortcode($post->post_content,'show-quiz'))){
wp_register_script('jquery','https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js',array(),'1.12.4',true);
wp_enqueue_script('jquery');
//Add defer/async attribute to scripts - to find their js-handle search files for: wp_register_script, and for: wp_enqueue_script
function add_async_defer_attribute($tag,$handle){
if ($handle == 'jquery') {return str_replace(' src',' defer="defer" src',$tag);}
return $tag;
}
add_filter('script_loader_tag','add_async_defer_attribute',12,2);
}
else {
wp_register_script('jquery','https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js',array(),'1.12.4',false);
wp_enqueue_script('jquery');
}
wp_deregister_script('wp-embed'); //never call wp-embed.min.js because we don't embed posts from other wp sites
wp_deregister_script('suffusion');
if (!has_shortcode($post->post_content,'video')){
wp_deregister_script('mediaelement');
wp_deregister_style('mediaelement');
wp_deregister_script('wp-mediaelement');
wp_deregister_style('wp-mediaelement');
}
// wp_deregister_style('magnific');
}
}
add_action('wp_enqueue_scripts','remove_ticks',12); //with late priority so that it is after the script was enqueued
- Then in footer.php we ensure to load “all crap” only after the page is rendered and ready for “first user interaction”, because this is how google assesses load time, says Patrick Sexton, https://varvy.com
if (window.addEventListener) window.addEventListener("load",downloadAllCrapOnload,false);
else if (window.attachEvent) window.attachEvent("onload",downloadAllCrapOnload);
else window.onload=downloadAllCrapOnload;
function downloadAllCrapOnload(){var csslast=document.createElement('link');csslast.rel='stylesheet';csslast.href='/wp-content/themes/gsd/custom/css-below-the-fold.css';csslast.type='text/css';var defercss=document.getElementsByTagName('link')[0];defercss.parentNode.insertBefore(csslast,defercss);if(!window.jQuery){var mya=document.createElement("script");mya.type='text/javascript';mya.async=false;mya.src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js";document.body.appendChild(mya);} var deferimg=document.getElementsByTagName('img');for(var i=0;i<deferimg.length;i++){if(deferimg[i].getAttribute('data-src')){deferimg[i].setAttribute('src',deferimg[i].getAttribute('data-src'));deferimg[i].removeAttribute('data-src');}} var myb=document.createElement("script");myb.type='text/javascript';myb.async=false;myb.src="https://checkout.stripe.com/checkout.js";document.body.appendChild(myb);var myc=document.createElement("script");myc.type='text/javascript';myc.async=false;myc.src="/wp-content/themes/gsd/scripts/mygsdorg_final.js";document.body.appendChild(myc);}
That’s it. All of this works like a charm. Took many months to figure out though.
–
Whenever you add a NEW plugin thereafter, you have to investigate its code / loading though. I guess that’s the job of a webmaster though?
Actually all this taught us to pre-evaluate any plugin before purchase whether it can match our new standards. Most plugins are programmed like crap, it seems. - hence why testing took months…
Hope this helps you too.