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

后端 未结 10 496
温柔的废话
温柔的废话 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:35

    A simplified method. Add to your functions.php file to make make JavaScript asynchronous in Wordpress

    // Make JavaScript Asynchronous in Wordpress
    add_filter( 'script_loader_tag', function ( $tag, $handle ) {    
        if( is_admin() ) {
            return $tag;
        }
        return str_replace( ' src', ' async src', $tag );
    }, 10, 2 );
    

提交回复
热议问题