defer="defer"

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

defer="defer"

Post by Vegan »

I was wondering for all the JaveScript in wordpress I was thinking why not load them all asynchronously so that Chome etc can grab all the parts at once and perk up rendering the site faster?

The idea is the have: <script defer="defer" src="myscript.js> which will load in parallel.

Now PHP can so some work such as: return str_replace( ' src', ' defer="defer" src', $tag );

Now some scripts already have defer, so I was wondering, a duplicate should be OK but to be smart, should I not check first?

Not sure the best spot yet to install my idea, thoughts? Found this code on https://wordpress.stackexchange.com/que ... 8335#38335

Code: Select all

add_filter( 'script_loader_tag', function ( $tag, $handle ) {

    if ( 'contact-form-7' !== $handle )
        return $tag;

    return str_replace( ' src', ' defer="defer" src', $tag );
}, 10, 2 );
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
Post Reply