Speed up WordPress in 2024 with these code snippets
The following 6 code snippets are really simple to implement and will take you under 5 minutes to do! The first 5 can just be pasted into your theme’s functions.php file for a quick fix. Number 6 would go into your site’s wp-config.php file, but is another simple copy and paste solution to give your WordPress site a speed boost. If you want to speed up wordpress even further, I added some extra tips after that just for good measure, but those may take a little more time to implement. If you want something that will make a huge impact quickly for a low cost, then I highly recommend trying out the FlyingPress plugin to really improve your core web vitals scores.
1. Remove query strings from static resources
Getting rid of the query strings on your css and js files not only allows caching of those static files, but it will also give your Google Pagespeed score a little boost.
/* Remove Query Strings from Static Resources /********************************************************/ 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 );
2. Remove emoji files
In WordPress 4.2, they introduced emojis. This added a bunch of javascript code just to add the ability to make smiley faces in your posts and comments. It’s bloated and unnecessary in my opinion, so it’s a safe thing to cut out in the interest of site speed improvements.
/* Remove Emoji included files since they will not be used /********************************************************/ function disable_wp_emojis() { // all actions related to emojis remove_action( 'admin_print_styles', 'print_emoji_styles' ); 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_filter( 'wp_mail', 'wp_staticize_emoji_for_email' ); remove_filter( 'the_content_feed', 'wp_staticize_emoji' ); remove_filter( 'comment_text_rss', 'wp_staticize_emoji' ); // filter to remove TinyMCE emojis add_filter( 'tiny_mce_plugins', 'disable_emojicons_tinymce' ); } add_action( 'init', 'disable_wp_emojis' ); //disable tinyMCE emojicons function function disable_emojicons_tinymce( $plugins ) { if ( is_array( $plugins ) ) { return array_diff( $plugins, array( 'wpemoji' ) ); } else { return array(); } } //remove DNS prefetch add_filter( 'emoji_svg_url', '__return_false' );
Bye bye Emojis!
3. Remove the default comments javascript code
If you are not using comments on your site at all, then there is no reason to be loading the code for it. The “comment-reply.js” file is loading on all your pages even if you have commenting disabled within your wordpress admin settings. You can use the code snippet below to get rid of it.
/* Remove the default commenting js code *********************************************************/ function speed_clean_header_hook(){ wp_deregister_script( 'comment-reply' ); } add_action('init','speed_clean_header_hook');
If you are setting up a new wordpress site and plan on having comments, you may want to try swapping it out and using Disqus commenting instead and just adding in the universal embed code they provide. This makes for a better page loading experience and to keeps the commenting bloat out of your wordpress database, but you lose the moderation from the dashboard. You can use the wordpress plugin version as well to keep moderation ability, but I’ve found that it bloats your database with metadata and can increase server load. To disable wordpress comments if you haven’t already, just go to Settings -> Discussion in your WordPress admin dashboard. Within there you will want to uncheck the box next to “Allow people to post comments on new articles”.
4. Remove wp_embed loading
The wp_embed.js file is what allows you to embed content from tweets, youtube videos, and other posts directly into your post using just the url. Some may find it useful if they do that sort of thing and can’t be bothered to use an embed code, but I just saw it as more unnecessary javascript code being loaded. So let’s get rid of it!
/* Remove wp_embed loading *********************************************************/ function speed_stop_loading_wp_embed() { if (!is_admin()) { wp_deregister_script('wp-embed'); } } add_action('init', 'speed_stop_loading_wp_embed');
5. Regulate the WordPress Heartbeat function
WordPress Heartbeat can be useful if you have a lot of people within the admin dashboard making edits often, but if you are the only one working on your site it isn’t really required. If you want to disable it entirely you can use the first snippet below. Just be aware that other plugins may hook into heartbeat for executing certain functions and disabling it entirely could break those functions.
/* Stop the WordPress Heartbeat function *********************************************************/ add_action( 'init', 'stop_heartbeat', 1 ); function stop_heartbeat() { wp_deregister_script('heartbeat'); }
I generally use this second code snippet below which keeps heartbeat active for people editing pages and posts as that is were I expect most overlap between users. The second function in there just slows the heartbeat down to execute every 60 seconds. You can edit this in many ways to change the heartbeat frequency and change which pages it executes on. You can exclude either the filter or action hook as well. If you only want to change the frequency for example, then you only need to add the filter hook and it’s matching function.
/* Regulate the WordPress Heartbeat function *********************************************************/ //stopping heartbeat everywhere except post edit pages add_action( 'init', 'stop_heartbeat', 1 ); function stop_heartbeat() { global $pagenow; if ( $pagenow != 'post.php' && $pagenow != 'post-new.php' ) wp_deregister_script('heartbeat'); } // limit the heartbeat call rate on pages where active function heartbeat_frequency( $settings ) { $heartbeat_frequency = 60; //60 seconds max $settings['interval'] = $heartbeat_frequency; return $settings; } add_filter( 'heartbeat_settings', 'heartbeat_frequency' );
6. Remove or Limit Post Revisions
Post revisions are automatically saved by default, but you may not need to keep so many revisions or may want to stop saving them altogether. It frees up space in the database. More revisions saved just means more bloat in your database. This change should be done within your wp-config.php file. In the below code snippet, I show how to disable revisions, and also how to limit the number of revisions. You would go with one or the other depending on your preference.
//Remove Post Revisions define( 'WP_POST_REVISIONS', FALSE ); //Limit Post Revisions define( 'WP_POST_REVISIONS', 3 );
Those are your 6 ways to Speed up WordPress in under 5 minutes. I bet it took you longer to read this post than to implement the changes. Continue on and get some more speed optimization tips!
Bonus Ways to Speed Up WordPress
7. Optimize Images
Smaller images files means less time to load them. You can either optimize your images before you upload them with something like TinyPNG or you could add in an image compression plugin like ShortPixel or WP SMUSH. If you go with ShortPixel, then you can even implement WebP format images which will get you way more in file-size reduction than regular image compression on supported browsers. You will need to combine it with Cache Enabler plugin to actually get them to display, but ShortPixel is needed to generate the images.
8. Use a CDN
A CDN caches your content on servers locations all over the world to more quickly serve up your site content no matter where the visitor is viewing it from. It can better handle traffic surges by dispersing the load and save on bandwidth. There are many very affordable options like Amazon Cloudfront and even some free options out there like Cloudflare depending on your site’s intent.
9. Disable Pingbacks and Trackbacks
I’ve found that the majority of pingbacks and trackbacks received are just spam so I treat them like spam and just get rid of them. They aren’t worth the resources they are using up. So to speed up wordpress in this instance we just need to alter another simple setting. Just go to Settings -> Discussion in your WordPress admin dashboard as we’ve done before. There you will want to uncheck the box next to “Allow link notifications from other blogs (pingbacks and trackbacks) on new articles”.
10. Remove Gravatars
If you aren’t using the default wordpress commenting you should definitely remove gravatars. If you are using the default wordpress commenting, then I still think you should consider removing gravatars if you want to cut off some load time. To do this you’ll ant to go back to Settings -> Discussion in the dashboard. Somewhere near the bottom of this page you will see the “Avatars” section and you can just uncheck the “Show Avatars” checkbox.
11. Optimize your Database
To optimize the database you can install a plugin like WP Optimize that will help clean out the non-essential database data to speed up WordPress. It can be run on a schedule to keep it clean without having to manually run it ever again. Think of this plugin as your WordPress database housekeeper.
12. Use a fast theme
There are a lot of pre-made themes out there that just have excess bloat because they try to do too much. Odds are you won’t be using every single one of the features they offer, so there is a lot of code loading that isn’t needed. I’m sure there are plenty of themes that aren’t using best practices as well. It’s best to start with a trusted, reliable, speed-optimized framework like the Genesis Framework. With that as a base, you only have to add in what you need and can keep the load times to a minimum.
Ready. Set. Speed!
You have all the tips to speed up WordPress, now go implement them on your site. Like right now, go do it! You can do half of these in under 5 minutes total, so start the clock.
Speak Your Mind