3,000th Posts Celebration - WordPress Performance Edition

Roger Marquez

Elite Member
Joined
Apr 17, 2017
Messages
5,126
Reaction score
7,816
giphy.gif


I couldn´t let this pass without posting something worthwhile for me 3,000th milestone on this amazing community. So, giving these past few months I´ve been research and learning a hell lot about how to optimize a WordPress-based websites page loading speed and, especially, how to do it WITHOUT plugins, I would like to share with you guys some stuff I found around the internet that has helped me with this.

Bear in mind, I don´t have a background in coding, programming, or even web development. I am, however, persistent and like to research a lot. So, before implementing any of these codes on any of your sites, be sure to backup everything and test.

These codes are meant to help to optimize any WordPress-based site by editing and manually adding some codes to the HTACCESS, function.php, and wp-config.php files. So, these codes are just for Apache servers.

I hope you can get some value out of this, cheers!!

HTACCESS FILE

FOR SPEED/PERFORMANCE

<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>

<IfModule mod_headers.c>
Header unset ETag
Header unset Pragma
Header unset Last-Modified
Header append Cache-Control "public, no-transform, must-revalidate"
Header set Last-modified "Tue, 26 Mar 2021 10:10:10 GMT"
</IfModule>

</IfModule>
# TN – DISABLE IMAGE HOTLINKIING START
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?sample.com [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ – [NC,F,L]
# TN – DISABLE IMAGE HOTLINKIING END
# BEGIN GZIP COMPRESSION
<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>
# END GZIP COMPRESSION# BEGIN DEFLATE COMPRESSION
<IfModule mod_filter.c>
AddOutputFilterByType DEFLATE "application/atom+xml" \
"application/javascript" \
"application/json" \
"application/ld+json" \
"application/manifest+json" \
"application/rdf+xml" \
"application/rss+xml" \
"application/schema+json" \
"application/vnd.geo+json" \
"application/vnd.ms-fontobject" \
"application/x-font-ttf" \
"application/x-javascript" \
"application/x-web-app-manifest+json" \
"application/xhtml+xml" \
"application/xml" \
"font/eot" \
"font/opentype" \
"image/bmp" \
"image/svg+xml" \
"image/vnd.microsoft.icon" \
"image/x-icon" \
"text/cache-manifest" \
"text/css" \
"text/html" \
"text/javascript" \
"text/plain" \
"text/vcard" \
"text/vnd.rim.location.xloc" \
"text/vtt" \
"text/x-component" \
"text/x-cross-domain-policy" \
"text/xml"
</IfModule>
# END DEFLATE COMPRESSION
<ifModule mod_headers.c>

Header set Connection keep-alive

</ifModule>
<IfModule mod_deflate.c>
# Compress HTML, CSS, JavaScript, Text, XML and fonts
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml

# Remove browser bugs (only needed for really old browsers)
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent
</IfModule>

FOR SECURITY
# Disable access to all file types except the following
Order deny,allow
Deny from all
<Files ~ ".(xml|css|js|jpe?g|png|gif|pdf|docx|rtf|odf|zip|rar)$">
Allow from all
</Files>
RewriteEngine on
RewriteCond %{HTTP_REFERER} example\.com [NC]
RewriteRule .* - [F]
# Blocks some XSS attacks
<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} (\|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule .* index.php [F,L]
</IfModule>
<Limit GET POST>
order allow,deny
deny from IP_ADDRESS_1
deny from IP_ADDRESS_2
allow from all
</Limit>
# Disable directory browsing
Options All -Indexes
<FilesMatch “^.*(error_log|wp-config\.php|php.ini|\.[hH][tT][aApP].*)$”>
Order deny,allow
Deny from all
</FilesMatch>
<files wp-config.php>
order allow,deny
deny from all
</files>
# Blocks all wp-includes folders and files
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^wp-admin/includes/ - [F,L]
RewriteRule !^wp-includes/ - [S=3]
RewriteRule ^wp-includes/[^/]+\.php$ - [F,L]
RewriteRule ^wp-includes/js/tinymce/langs/.+\.php - [F,L]
RewriteRule ^wp-includes/theme-compat/ - [F,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*)?wp-login\.php(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^(.*)?wp-admin$
RewriteCond %{REMOTE_ADDR} !IP_ADDRESS_1
RewriteRule ^(.*)$ – [R=403,L]
</IfModule>

FOR REDIRECTS
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ https://new.domain.com/$1 [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule (.*) https://www.domain.com/$1 [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule (.*) https://domain.com/$1 [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]

</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
Redirect 301 /domain.com /new.domain.com

</IfModule>

FUNCTION.PHP FILE
function add_img_size($content){
$pattern = '/<img [^>]*?src="(https?:\/\/[^"]+?)"[^>]*?>/iu';
preg_match_all($pattern, $content, $imgs);
foreach ( $imgs[0] as $i => $img ) {
if ( false !== strpos( $img, 'width=' ) && false !== strpos( $img, 'height=' ) ) {
continue;
}
$img_url = $imgs[1][$i];
$img_size = @getimagesize( $img_url );

if ( false === $img_size ) {
continue;
}
$replaced_img = str_replace( '<img ', '<img ' . $img_size[3] . ' ', $imgs[0][$i] );
$content = str_replace( $img, $replaced_img, $content );
}
return $content;
}
add_filter('the_content','add_img_size');
/*defer JS parsing */

function defer_parsing_of_js ( $url )
{
if ( FALSE === strpos( $url, '.js' ) )
return $url;
if ( strpos( $url, 'jquery.js' ) )
return $url;
if ( strpos( $url, 'wp-custom-countdown' ) )
return $url;
return "$url' defer ";
}

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

/*code ends */
function wpdocs_dequeue_dashicon() {
if (current_user_can( ‘update_core’ )) {
return;
}
wp_deregister_style(‘dashicons’);
}
add_action( ‘wp_enqueue_scripts’, ‘wpdocs_dequeue_dashicon’ );
add_action( ‘init’, ‘stop_heartbeat’, 1 );
function stop_heartbeat() {
wp_deregister_script(‘heartbeat’);
}
function disable_pingback( &$links ) {
foreach ( $links as $l => $link )
if ( 0 === strpos( $link, get_option( ‘home’ ) ) )
unset($links[$l]);
}
add_action( ‘pre_ping’, ‘disable_pingback’ );
/*This allows to upload webp files */
function webp_file_ext( $types, $file, $filename, $mimes ) {
if ( false !== strpos( $filename, '.webp' ) ) {
$types['ext'] = 'webp';
$types['type'] = 'image/webp';
}
return $types;
}
add_filter( 'wp_check_filetype_and_ext', 'webp_file_ext', 10, 4 );

function webp_file_upload( $mimes ) {
$mimes['webp'] = 'image/webp';
return $mimes;
}
add_filter( 'upload_mimes', 'webp_file_upload' );

/*Preview Webp files */
function preview_webp_thumnail($result, $path) {
if ($result === false) {
$displayable_image_types = array( IMAGETYPE_WEBP );
$info = @getimagesize( $path );

if (empty($info)) {
$result = false;
} elseif (!in_array($info[2], $displayable_image_types)) {
$result = false;
} else {
$result = true;
}
}

return $result;
}
add_filter('file_is_displayable_image', 'preview_webp_thumnail', 10, 2);
/**
* Disable the emoji's
*/
function disable_emojis() {
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );
remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
add_filter( 'tiny_mce_plugins', 'disable_emojis_tinymce' );
}
add_action( 'init', 'disable_emojis' );

/**
* Filter function used to remove the tinymce emoji plugin.
*
* @Param array $plugins
* @return array Difference betwen the two arrays
*/
function disable_emojis_tinymce( $plugins ) {
if ( is_array( $plugins ) ) {
return array_diff( $plugins, array( 'wpemoji' ) );
} else {
return array();
}
}
function deregister_qjuery() {
if ( !is_admin() ) {
wp_deregister_script('jquery');
}
}
add_action('wp_enqueue_scripts', 'deregister_qjuery');
function remove_cssjs_ver( $src ) {
if( strpos( $src, ‘?ver=’ ) )
$src = remove_query_arg( ‘ver’, $src );
return $src;
}
add_filter( ‘style_loader_src’, ‘remove_cssjs_ver’, 10, 2 );
add_filter( ‘script_loader_src’, ‘remove_cssjs_ver’, 10, 2 );

Some of the code requires some editing, meaning, you´ll need to add your site´s domain or the image's actual width and height. Other than that, I think all the codes are self-explanatory.

I will also share with you guys on the first post my own personal list of my favorite resources build around WordPress Speed/Performance Optimization.

And, as always, cheers and stay safe!!
 
My favorite BHW WordPress speed optimization resources...

[ebook + 49 videos] Crazy Fast WordPress Sites! by @Shree
WORDPRESS OPTIMIZATION GUIDE! FROM 3 TO 1 SECOND LOAD-TIME by @FatBee

My favorite YouTube videos on WP speed optimization…


My favorite resources build around wordpress speed/performance optimization….

https://wpjohnny.com/ultimate-wordpress-speed-optimization-guide/https://mythemeshop.com/blog/wordpress-speed-optimization/#intermediatehttps://onlinemediamasters.com/#https://wpspeedmatters.com/https://www.sertmedia.com/blog/https://www.shaytoder.com/blog/https://ulement.com/tutorial/how-to-speed-optimize-wordpress-website/https://woorkup.com/https://daan.dev/wordpress/https://varvy.com/pagespeed/http://pagepipe.com/best-of-pagepipe/https://www.samunderwood.co.uk/blog/site-speed-tips/https://help.gulshankumar.net/search?q=speed category:6https://seosly.com/google-page-experience-audit/

Some speed test tools I have bookmarked….

https://developers.google.com/speed/pagespeed/insights/https://gtmetrix.com/https://www.webpagetest.org/https://yellowlab.tools/https://tools.pingdom.com/https://tools.keycdn.com/performancehttp://www.solvedns.com/dnsspeedtest/https://www.dotcom-tools.com/website-speed-test.aspx

Image optimization tools

https://squoosh.app/https://tinypng.com/https://compress-or-die.com/https://ezgif.com/jpg-to-webphttps://image.online-convert.com/convert-to-webphttps://imageoptim.com/mac
Font optimization tool

https://transfonter.org/

CSS / JS optimization tools

https://jscompress.com/https://cssminifier.com/https://www.minifier.org/https://pegasaas.com/critical-path-css-generator/https://www.toptal.com/developers/css/sprite-generator/


Cheeeers!!!
 
Congratulations on 3000+ posts and Thank You.
 
Congrats and thank you for sharing your valuable experiences.
 
Congrats, you always share something worthwhile.
 
Back
Top