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

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

    Or more universal way:

    function add_async_forscript($url)
    {
        if (strpos($url, '#asyncload')===false)
            return $url;
        else if (is_admin())
            return str_replace('#asyncload', '', $url);
        else
            return str_replace('#asyncload', '', $url)."' async='async"; 
    }
    add_filter('clean_url', 'add_async_forscript', 11, 1);
    

    so you can add async to any script without code changes, just add #asyncload to script url as:

    wp_enqueue_script('dcsnt', '/js/jquery.social.media.tabs.1.7.1.min.js#asyncload' )
    

提交回复
热议问题