How to Improve WordPress Speed Optimization

In this article, we are going to see some of the most popular WordPress speed optimization tips, which can be done without any plugins on our WordPress site. Although the WordPress core is not large, the themes and plugins you use may slow down the overall page load time. There are numerous methods for speeding up your WordPress website.

Learn WordPress Theme Development Course with Bootstrap 5 (2021).

Learn to Build the Fastest & Advanced Custom WordPress Theme from scratch with Bootstrap 5, ACF, & Custom Post Type. WordPress Theme Development Course with Bootstrap 5 [2021]

Sign-up Now

As an example:

Using a CDN (Content Delivery Network) Taking advantage of browser and server caching Hosting on a performance-optimized server

Making use of a light-weight theme for better WordPress speed optimization

However, did you know that there are many things included in the WordPress core that you may not use and that taking necessary action on them may save a few bytes to make the website load faster? The following can be accomplished in two ways: first, by utilizing a plugin, and second, by inserting a few lines of code into functions.php.

If something can be done with code addition/modification in an existing file, I prefer not to use a plugin.

The Best Practice for WordPress speed optimization

Make a backup of the file you intend to modify so that you can roll back quickly if something goes wrong. Unless otherwise specified, all of the following codes must be added to functions.php.

Try Disabling Contact Form 7 JS/CSS

Are you using Contact Form 7 and have noticed that their CSS/JavaScript files are being loaded on every page? You’re not alone. The good news is that you can disable it by using the code below.

add_filter( 'wpcf7_load_js', '__return_false' );
add_filter( 'wpcf7_load_css', '__return_false' );

Disable the Dashicons on the front end.

Dashicons are used in the admin console, and if you are not using them to load any icons on the front-end, you should disable them.
By including the code below, dashicons.min.css will no longer load on the front end.

function wpdocs_dequeue_dashicon() {
        if (current_user_can( 'update_core' )) {
            return;
        }
        wp_deregister_style('dashicons');
}
add_action( 'wp_enqueue_scripts', 'wpdocs_dequeue_dashicon' );

Heartbeat should be disabled.

WordPress uses the heartbeat API to communicate with a server from a browser by repeatedly calling admin-ajax.php. If you’re using shared hosting, this may slow down overall page load time and increase CPU utilization. If you do not need to use the heartbeat API, you can disable it by adding the following code.

add_action( 'init', 'stop_heartbeat', 1 );

function stop_heartbeat() {
    wp_deregister_script('heartbeat');
}

Disable or restrict post-revisions

Post revisions in WordPress are not new, and they are useful for restoring a post if the browser crashes or the network goes down. But consider how many times this has occurred. WordPress saves revisions for each draft or published post by default, which can clog the database. You can either disable it completely or limit the number of revisions that can be saved.

Insert the following code into the wp-config.php file.

Post Revision Examples:

/* Disable Post Revision */
define('WP_POST_REVISIONS', false);

/* Setting the Limit to Post Revision */
define('WP_POST_REVISIONS', 2);

Pingback: Disable Self Pingback

I’m not sure why you need the self-pingback details on your blog post, and I know I’m not the only one who gets annoyed. If you are, the code below will assist you.

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' );

JQuery Migrate should be removed.

Since version 3.6, WordPress has included JQuery migration. This isn’t necessary if you’re using the most recent version of JQuery and your themes/plugins are compatible with it. Add the following code to prevent jquery-migrate.min.js from loading.

function deregister_qjuery() { 
    if ( !is_admin() ) {
        wp_deregister_script('jquery');
    }
} 
add_action('wp_enqueue_scripts', 'deregister_qjuery');

WLManifest Link should be removed.

Do you use Windows Live Writer’s tagging support? If it isn’t already there, add it below.

remove_action( 'wp_head', 'wlwmanifest_link' ) ;

Try Hiding The WordPress Version.

This has less to do with performance and more to do with mitigating the risk of information leakage. WordPress includes a meta name generator with the version details by default, which is visible in the source code and HTTP header.

Add the following code to remove the WP version.

remove_action( 'wp_head', 'wp_generator' ) ;

Turn off XML-RPC.

Is it necessary for you to use the WordPress API (XML-RPC) to publish/edit/delete a post, edit/list comments, or upload a file? Having XML-RPC enabled and not properly hardened may also lead to DDoS and brute force attacks. If you don’t need it, disable it by adding the code below.

add_filter('xmlrpc_enabled', '__return_false');

Embeds should be disabled.

WordPress 4.4 introduced the oEmbed feature, which allows any site to remotely embed a WordPress post, and it looks like this. The following code will prevent others from embedding your blog post and will disable the loading of related JS files.

function disable_embed(){
    wp_dequeue_script( 'wp-embed' );
}
add_action( 'wp_footer', 'disable_embed' );

Remove the short link

WordPress version 3 added shortlink (a shorter link to a web page address) to the header code. As an example:

<link rel="shortlink" href="https://geekflare.com/?p=187" />

If you are not using shortlinks for any functionality, you can remove them by adding the following.

remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);

Emoticons should be turned off.

Remove emoji-related code from WordPress that was recently added to support emoticons in an older browser.

remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );

RSD Links Must Be Removed

RSD (Really Simple Discovery) is required if you want to use an XML-RPC client, pingback, and so on. If you don’t need pingback or a remote client to manage your posts, remove this unnecessary header by adding the following code.

remove_action( 'wp_head', 'rsd_link' ) ;

Query strings should be removed.

If you have performed a load time analysis on your website, you may have come across a recommendation to remove query strings from static resources (CSS, JS files). Having query strings in the files may cause CDN to not cache the files, resulting in you not taking advantage of all caching benefits.

Add the following code to remove the query strings.

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

Master with Our Course!

Hrs of course contents, Modules, and lessons. Learn from industry experts. Perfect for .

Enroll now to Get Started!


Source link