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

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

    I believe it's bad practice to defer/async WordPress jQuery. Better solution would be to exclude jQuery from the filter:

    if (!is_admin()) {
        add_filter( 'script_loader_tag', function ( $tag, $handle ) {    
            if ( strpos( $tag, "jquery.js" ) || strpos( $tag, "jquery-migrate.min.js") ) {
                return $tag;
            }
            return str_replace( ' src', ' async src', $tag );
        }, 10, 2 );
    }
    

    You can use defer instead of async

提交回复
热议问题