API
The page load time depends on the load time of 3rd party resources. To reduce page load time, you can use several techniques of preloading.
add_action('wp_head', function(){
?>
<link rel='dns-prefetch' href='https://www.googletagmanager.com' />
<link rel='dns-prefetch' href='https://connect.facebook.net' />
<link rel='dns-prefetch' href='https://cdn.autoads.asia' />
<link rel='dns-prefetch' href='https://www.google-analytics.com' />
<link rel='dns-prefetch' href='https://www.youtube.com' />
<link rel='dns-prefetch' href='https://api.autoads.asia' />
<link rel='dns-prefetch' href='https://www.facebook.com' />
<link rel='dns-prefetch' href='htpss://googleads.g.doubleclick.net' />
<link rel='dns-prefetch' href='https://maps.googleapis.com' />
<link rel='dns-prefetch' href='https://scontent-ort2-1.xx.fbcdn.net' />
<link rel='dns-prefetch' href='https://i.ytimg.com' />
<link rel='dns-prefetch' href='https://yt3.ggpht.com' />
<link rel='dns-prefetch' href='https://static.doubleclick.net' />
<link rel='preconnect' href='https://fonts.gstatic.com' crossorigin />
<?php
});
For multilingual sites, in url has contain language code ex: http://example.com/en/post-1
So you need to add all language codes that your site using to this filter:
add_filter('hpp_url_langs', function(){return 'en|vi|ja';});
Remember that optimization will only work in the frontend (not logged user) and It makes no sense to wp-admin.
You don't want to turn on optimization on certain pages for some reason. Ex: checkout page, cart page..
add_filter('hpp_should_lazy', function($v){
$uris=[
'/thanh-toan/','/gio-hang/',
'/checkout/','/cart/',
'/login/','/dang-nhap/',
'/order-received/',
];
if(!is_user_logged_in()) {
foreach($uris as $uri) if(stripos($_SERVER['REQUEST_URI'], $uri)!==false ) return false;
}
else $v=false;
return $v;
});
Using the function
hpp_shouldlazy()
to known whether the current page is optimally enabled or not:add_action('wp_head', function(){
if(!hpp_shouldlazy()) echo '<script>if(typeof $=="undefined" && typeof jQuery!="undefined")$=jQuery;</script>';
});
When you press 'purge all' in settings > wp2speed , then all files in the
wp-content/cache/
& wp-content/mmr
folders will be deleted. When you reload the page you may feel a bit slower, because the merge files & cache files being created.If you want to do something more during clean the cache process, use the hook:
add_action('hpp_purge_cache', function(){
//do something
@mkdir(WP_CONTENT_DIR.'/cache/caos-analytics', 0755,true);
file_put_contents(WP_CONTENT_DIR.'/cache/caos-analytics/analytics.js','');
});
Modify any html in buffer output.
add_filter('hpp_prepare_buffer_html', function($html, $merged=null){
$html = str_replace('http://', 'https://', $html);
$html = str_replace('http:\/\/', 'https:\/\/', $html);
return $html;
},10, 2);
If you plan to use cdn, ex:
https://net-soledad-gavu0xo8w0ybxpt6g.netdna-ssl.com/wp-content/uploads/2017/07/[email protected]
use this hook to replace with your cdn for css,js files in mmr folder.//change to your cdn
add_filter('hpp_cache_url', function($url){
$url = str_replace(siteurl(), 'your-cdn', $url);
return $url;
});
Last modified 2yr ago