How do I defer or async this WordPress javascript snippet to load lastly for faster page load times?

后端 未结 10 504
温柔的废话
温柔的废话 2020-12-07 09:28

I have various javascripts that are necessary plugins in one of my WordPress domains, and I know where in the php file it\'s called from.

I\'m taking every measure I

10条回答
  •  日久生厌
    2020-12-07 09:49

    This blog post links to two plugins of interest:

    Asynchronous Javascript
    Improve page load performance by asynchronously loading javascript using head.js

    WP Deferred Javascripts
    Defer the loading of all javascripts added with wp_enqueue_scripts, using LABJS (an asynchronous javascript library).

    Haven't tested them but checked the code and they do pretty fancy stuff with WordPress scripts enqueuing process.

    But then WPSE comes to rescue:

    // Adapted from https://gist.github.com/toscho/1584783
    add_filter( 'clean_url', function( $url )
    {
        if ( FALSE === strpos( $url, '.js' ) )
        { // not our file
            return $url;
        }
        // Must be a ', not "!
        return "$url' defer='defer";
    }, 11, 1 );
    

提交回复
热议问题