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

How to defer javascript from within the functions.php file without losing all styling?

programmeradmin1浏览0评论

I would like to increase performance of wordpress site by deferring javascript on the site. However, when I do this, it loses all styling. Is there a way to be more specific on which styles to render and not?

I'm using the below code in a functions.php file to implement this. It seems to work, but the website loses all of it's stylings:

function defer_parsing_js($url) {
//Add the files to exclude from defer. Add jquery.js by default
    $exclude_files = array('jquery.js');
//Bypass JS defer for logged in users
    if (!is_user_logged_in()) {
        if (false === strpos($url, '.js')) {
            return $url;
        }

        foreach ($exclude_files as $file) {
            if (strpos($url, $file)) {
                return $url;
            }
        }
    } else {
        return $url;
    }
    return "$url' defer='defer";
}

add_filter('clean_url', 'defer_parsing_js', 11, 1);

Is there a better method I do not know about? Any help is appreciated.

发布评论

评论列表(0)

  1. 暂无评论