最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

filters - Unable to defer loading of jquery

programmeradmin0浏览0评论

I have two render blocking JS from wordpress core. jquery.js and jquery-migrate.min.js. Following this answer on WPSE I was able to add `defer="defer"' to the script tag that calls for jquery-migrate and bootstrap, but doing the same for 'jquery' does not add the defer property to the script tag that calls for jquery.js

  add_filter( 'script_loader_tag', function ( $tag, $handle ) {
    $handlesToDefer = array('jquery-migrate', 'bootstrap', 'jquery');

    if ( !in_array($handle, $handlesToDefer) )
        return $tag;

    return str_replace( ' src', ' defer="defer" src', $tag );
 }, 1, 2 );

I have two render blocking JS from wordpress core. jquery.js and jquery-migrate.min.js. Following this answer on WPSE I was able to add `defer="defer"' to the script tag that calls for jquery-migrate and bootstrap, but doing the same for 'jquery' does not add the defer property to the script tag that calls for jquery.js

  add_filter( 'script_loader_tag', function ( $tag, $handle ) {
    $handlesToDefer = array('jquery-migrate', 'bootstrap', 'jquery');

    if ( !in_array($handle, $handlesToDefer) )
        return $tag;

    return str_replace( ' src', ' defer="defer" src', $tag );
 }, 1, 2 );

Share Improve this question edited Sep 30, 2019 at 14:13 Chetan Vaghela 2,4084 gold badges10 silver badges16 bronze badges asked Sep 30, 2019 at 13:42 OctaviaLoOctaviaLo 1398 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0
add_filter( 'script_loader_tag', function ( $tag, $handle ) {
$handlesToDefer = array('jquery-migrate', 'bootstrap', 'jquery-core');

if ( !in_array($handle, $handlesToDefer) )
    return $tag;

return str_replace( ' src', ' defer="defer" src', $tag );
发布评论

评论列表(0)

  1. 暂无评论