The Ultimate Snippets to Boost WordPress Speed & Performance

Do you want to speed up your WordPress site?

Follow These Tips and Snippets to boost WordPress performance and speed up your website.

Remove comment-reply.min.js file from all pages except posts.

<?php
/**
*Remove comment-reply.min.js from all pages except posts
*/
add_action( 'wp_enqueue_scripts', 'comment_reply_hook' );
function comment_reply_hook() {
if ( !is_single()) {
wp_deregister_script( 'comment-reply' );
}

Disable Emojis

One easy optimization is to disable emojis from loading. Emojis are little icons used to express ideas or emotions. While these icons are fun and all, are they really necessary for your WordPress site? Especially if you are a business, these are simply adding additional load time which is unnecessary.

<?php
/**
 * 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_filter( 'wp_resource_hints', 'disable_emojis_remove_dns_prefetch', 10, 2 );
}
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();
 }
}

/**
 * Remove emoji CDN hostname from DNS prefetching hints.
 *
 * @param array $urls URLs to print for resource hints.
 * @param string $relation_type The relation type the URLs are printed for.
 * @return array Difference betwen the two arrays.
 */
function disable_emojis_remove_dns_prefetch( $urls, $relation_type ) {
 if ( 'dns-prefetch' == $relation_type ) {
 /** This filter is documented in wp-includes/formatting.php */
 $emoji_svg_url = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/2/svg/' );

$urls = array_diff( $urls, array( $emoji_svg_url ) );
 }

return $urls;
}

Disable Embeds

WordPress has been an oEmbed consumer for a long time.
And this loads on every single page. While this file is only 1.7 KB, things like these add up over time. The request itself is sometimes a bigger deal than the content download size.

Or you could also use the wp_dequeue_script function.

function disable_embeds_code_init() {

 // Remove the REST API endpoint.
 remove_action( 'rest_api_init', 'wp_oembed_register_route' );

 // Turn off oEmbed auto discovery.
 add_filter( 'embed_oembed_discover', '__return_false' );

 // Don't filter oEmbed results.
 remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 );

 // Remove oEmbed discovery links.
 remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );

 // Remove oEmbed-specific JavaScript from the front-end and back-end.
 remove_action( 'wp_head', 'wp_oembed_add_host_js' );
 add_filter( 'tiny_mce_plugins', 'disable_embeds_tiny_mce_plugin' );

 // Remove all embeds rewrite rules.
 add_filter( 'rewrite_rules_array', 'disable_embeds_rewrites' );

 // Remove filter of the oEmbed result before any HTTP requests are made.
 remove_filter( 'pre_oembed_result', 'wp_filter_pre_oembed_result', 10 );
}

add_action( 'init', 'disable_embeds_code_init', 9999 );

function disable_embeds_tiny_mce_plugin($plugins) {
    return array_diff($plugins, array('wpembed'));
}

function disable_embeds_rewrites($rules) {
    foreach($rules as $rule => $rewrite) {
        if(false !== strpos($rewrite, 'embed=true')) {
            unset($rules[$rule]);
        }
    }
    return $rules;
}
function my_deregister_wp_embed(){
 wp_dequeue_script( 'wp-embed' );
}
add_action( 'wp_footer', 'my_deregister_wp_embed' );

Disable RSS Feed

By default WordPress generates all kinds of RSS feeds that are built in.
It also generates them for your categories, tags, comments, etc.

function itsme_disable_feed() {
 wp_die( __( 'No feed available, please visit our <a href="'. esc_url( home_url( '/' ) ) .'">homepage</a>!' ) );
}

add_action('do_feed', 'itsme_disable_feed', 1);
add_action('do_feed_rdf', 'itsme_disable_feed', 1);
add_action('do_feed_rss', 'itsme_disable_feed', 1);
add_action('do_feed_rss2', 'itsme_disable_feed', 1);
add_action('do_feed_atom', 'itsme_disable_feed', 1);
add_action('do_feed_rss2_comments', 'itsme_disable_feed', 1);
add_action('do_feed_atom_comments', 'itsme_disable_feed', 1);
remove_action( 'wp_head', 'feed_links_extra', 3 );
remove_action( 'wp_head', 'feed_links', 2 );

Remove jQuery Migrate

If you are trying to increase the performance of your site you may
have noticed jQuery Migrate loading on your site.
The snippet below will remove jQuery Migrate from your site.
After removing jQuery Migrate take a look at a few of your pages to ensure the site is still functioning correctly.

<?php
//Remove jQuery migrate
function smartwp_remove_jquery_migrate( $scripts ) {
 if ( !is_admin() && !empty( $scripts->registered['jquery'] ) ) {
 $scripts->registered['jquery']->deps = array_diff( $scripts->registered['jquery']->deps, ['jquery-migrate'] );
 }
}
add_action('wp_default_scripts', 'smartwp_remove_jquery_migrate');

Remove Query Strings from Static Resources 

If you’ve tested the speed of your site on a tool like Pingdom or GTmetrix you may have seen the message to remove query strings from your static resources.
here is a snippet do remove those query strings.

<?php
//Remove Query Strings From Static Resources
function arctica_remove_query_strings_from_static_resources( $src ) {
 if( strpos( $src, '?v=' ) ){
 $src = remove_query_arg( 'v', $src );
 }
 if( strpos( $src, '?ver=' ) ){
 $src = remove_query_arg( 'ver', $src );
 }
 return $src;
}
add_filter( 'script_loader_src', 'arctica_remove_query_strings_from_static_resources', 999 );
add_filter( 'style_loader_src', 'arctica_remove_query_strings_from_static_resources', 999 );

Summary

As you can probably tell, we are obsessed with all the different ways you can speed up WordPress. Having a fast site helps boost your rankings, improves crawlability for search engines, improves conversion rates, increases time on site, and decreases your bounce rate.


Now it’s your turn. How do you speed up WordPress? Do you have any tricks up your sleeve?