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

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

    Something to add the clean_url filter solution, make sure to validate to use it only on the font-end maybe using if( ! is_admin() ){} popular plugins like ACF might give you a headache.

    Update

    Here is my modified version of the solution:

    if( ! is_admin() ){
      add_filter( 'clean_url', 'so_18944027_front_end_defer', 11, 1 );
      function so_18944027_front_end_defer( $url ) {
        if ( FALSE === strpos( $url, '.js' ) )
        { // not our file
            return $url;
        }
        // Must be a ', not "!
        return "$url' defer='defer";
      }
    }
    

提交回复
热议问题