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.