Speed Optimisation on WordPress
- سُلَيْمَان بْن دَاوُوْد

- Jan 12, 2022
- 3 min read
The speed optimisation on word press depends on multiple factors we will discuss them here
1- Reduce External Script Request
This can be done by locally including css from same server as of website NOTE: you will have to download and keep fonts in same folder as the fonts are relative to CSS on CDN
Preferred Code Below
/**
* Enqueue scripts and styles.
*/
function mccq_scripts() {
wp_enqueue_style( 'mccq-style', get_stylesheet_uri(), array(), _S_VERSION );
wp_style_add_data( 'mccq-style', 'rtl', 'replace' );
wp_enqueue_style( 'mccq-slick', get_template_directory_uri() . '/css/slick.css');
wp_enqueue_style( 'mccq-slick-theme',get_template_directory_uri() . '/css/slick-theme.css');
wp_enqueue_style( 'mccq-aos', get_template_directory_uri() . '/css/aos.css');
// Template Main CSS File
wp_enqueue_style( 'mccq-style-css', get_template_directory_uri() . '/app/css/style.css');
// JS - Vendor js Files
wp_deregister_script('jquery');
wp_enqueue_script( 'jquery', get_template_directory_uri() . '/js/jquery.min.js');
wp_enqueue_script( 'mccq-slick', get_template_directory_uri() . '/js/slick.min.js', array('jquery') , '', true);
wp_enqueue_script( 'jquery-validate', get_template_directory_uri() . '/js/jquery.validate.min.js',array('jquery') , '', true );
wp_enqueue_script( 'developer-js', get_template_directory_uri() . '/js/developer.js', array('jquery'), true );
wp_enqueue_script( 'mccq-aos', get_template_directory_uri() . '/js/aos.js');
wp_enqueue_script( 'mccq-main-js', get_template_directory_uri() . '/js/main.js',array('jquery'),'' , true);
$quote_data = array(
'siteurl' => site_url(),
'adminurl' => admin_url(),
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'templateurl'=> get_template_directory_uri()
);
wp_localize_script( 'developer-js', 'quote_obj', $quote_data );
}
add_action( 'wp_enqueue_scripts', 'mccq_scripts' );Previous Code acting as Render Blocking Payload due to external CDN inclusion

2- Defer Parsing of JS
/*Defer Parsing of JS*/
if ( ! is_admin() ) {
function defer_parsing_of_js ( $url ) {
if ( FALSE === strpos( $url, '.js' ) ) return $url;
if ( strpos( $url, 'jquery.min.js' ) || strpos( $url, 'aos.js' ) ) return $url;
return "$url' defer='defer";
}
add_filter( 'clean_url', 'defer_parsing_of_js', 11, 1 );
}3- Use Mod Deflate
<IfModule mod_deflate.c>
AddType x-font/woff .woff
AddType x-font/ttf .ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE x-font/ttf
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE font/opentype font/ttf font/eot font/otf
</IfModule>4- Use Mod Expires
<FilesMatch "\.(webm|ogg|mp4|ico|pdf|flv|jpg|jpeg|png|gif|webp|js|css|swf|x-html|css|xml|js|woff|woff2|otf|ttf|svg|eot)(\.gz)?$">
<IfModule mod_expires.c>
AddType application/font-woff2 .woff2
AddType application/x-font-opentype .otf
ExpiresActive On
ExpiresDefault A0
ExpiresByType video/webm A10368000
ExpiresByType video/ogg A10368000
ExpiresByType video/mp4 A10368000
ExpiresByType image/webp A10368000
ExpiresByType image/gif A10368000
ExpiresByType image/png A10368000
ExpiresByType image/jpg A10368000
ExpiresByType image/jpeg A10368000
ExpiresByType image/ico A10368000
ExpiresByType image/svg+xml A10368000
ExpiresByType text/css A10368000
ExpiresByType text/javascript A10368000
ExpiresByType application/javascript A10368000
ExpiresByType application/x-javascript A10368000
ExpiresByType application/font-woff2 A10368000
ExpiresByType application/x-font-opentype A10368000
ExpiresByType application/x-font-truetype A10368000
</IfModule>
</FilesMatch><IfModule mod_expires.c>
ExpiresActive On
# Images
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/webp "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
ExpiresByType image/x-icon "access plus 1 year"
# Video
ExpiresByType video/webm "access plus 1 year"
ExpiresByType video/mp4 "access plus 1 year"
ExpiresByType video/mpeg "access plus 1 year"
# Fonts
ExpiresByType font/ttf "access plus 1 year"
ExpiresByType font/otf "access plus 1 year"
ExpiresByType font/woff "access plus 1 year"
ExpiresByType font/woff2 "access plus 1 year"
ExpiresByType application/font-woff "access plus 1 year"
# CSS, JavaScript
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
# Others
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType image/vnd.microsoft.icon "access plus 1 year"
</IfModule>5- Use Mod Headers
<FilesMatch "index\.(html|htm)$">
AddDefaultCharset UTF-8
<ifModule mod_headers.c>
FileETag None
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Mon, 29 Oct 1923 20:30:00 GMT"
</ifModule>
</FilesMatch>6- Reduce Theme Lookup Path
Declare path of theme in wp-config.php
define('TEMPLATEPATH', '/nas/content/live/bribow67/wp-content/themes/ca2020_dev');
define('STYLESHEETPATH', '/nas/content/live/bribow67/wp-content/themes/ca2020_dev'); 7- Declare Some Variables in wp-config.php
define( 'COOKIE_DOMAIN', 'www.consumeracquisition.com' );
define( 'COMPRESS_CSS', true );
define( 'COMPRESS_SCRIPTS', true );
define( 'ENFORCE_GZIP', true );
define( 'ENABLE_CACHE', true);
define( 'WP_MEMORY_LIMIT', '1024M');8- DNS Prefetch
/*Add DNS Prefetch*/
function consumer_dns_prefetch() {
echo '<meta http-equiv="x-dns-prefetch-control" content="on">
<link rel="dns-prefetch" href="//fonts.googleapis.com" />
<link rel="dns-prefetch" href="//fonts.gstatic.com" />
<link rel="dns-prefetch" href="//vjs.zencdn.net" />
<link rel="dns-prefetch" href="//connect.facebook.net" />
<link rel="dns-prefetch" href="//www.facebook.com" />
<link rel="dns-prefetch" href="//cdnjs.cloudflare.com" />
<link rel="dns-prefetch" href="//f.vimeocdn.com" />
<link rel="dns-prefetch" href="//i.vimeocdn.com" />
<link rel="dns-prefetch" href="//fresnel.vimeocdn.com" />
<link rel="dns-prefetch" href="//player.vimeo.com" />
<link rel="dns-prefetch" href="//cdn.jsdelivr.net" />
<link rel="dns-prefetch" href="//stackpath.bootstrapcdn.com" />
<link rel="dns-prefetch" href="//maxcdn.bootstrapcdn.com" />
<link rel="dns-prefetch" href="//cdnjs.cloudflare.com" />
<link rel="dns-prefetch" href="//www.googletagmanager.com" />
<link rel="dns-prefetch" href="//www.google-analytics.com" />
<link rel="dns-prefetch" href="//www.consumeracquisition.com" />
<link rel="dns-prefetch" href="//0.gravatar.com/" />
<link rel="dns-prefetch" href="//2.gravatar.com/" />
<link rel="dns-prefetch" href="//1.gravatar.com/" />
<link rel="dns-prefetch" href="//in.hotjar.com/" />
<link rel="dns-prefetch" href="//conacq.zendesk.com/" />
<link rel="dns-prefetch" href="//ekr.zdassets.com/" />
<link rel="dns-prefetch" href="//static.zdassets.com/" />
<link rel="dns-prefetch" href="//vars.hotjar.com/" />
<link rel="dns-prefetch" href="//conacq.zendesk.com/" />';
}
add_action('wp_head', 'consumer_dns_prefetch', 0);
/*End Add DNS Prefetch*/
Comments