May 15, 2020

How to remove unused CSS JavaScript files in wordPress

Among the many reasons for the overwhelming popularity of WordPress on the web, the developer community is that there are a huge number of plugins and themes available for building a custom website with relatively little effort and investment. However, when several scripts are working on the site, it can slow down its performance and loading time.

Even though some of the scripts do not need to be loaded at the same time, they do so and end up preventing users from being able to view the website contents quickly leading to abandoning the site out of frustration. Luckily, there are quite a few ways in which these render-blocking JavaScript and CSS files can be removed to permit the website to load quickly and deliver the contents to the viewers.

Render-Blocking JavaScript and CSS Explained

Whenever a website loads in a web browser, it requests all scripts to load in a queue. If due to any reason, the queue of scripts refuses to clear up, the website does not display. These scripts that refuse to load are termed as render-blocking JavaScript and CSS files. The time for the site to load fully depends on the length of the queue. Very often, many of the scripts stuck in the queue are not even necessary for allowing the viewer to see the website at that point in time and can very easily be made to wait to run until the website has loaded fully.

Disadvantages of Render-Blocking JavaScript and CSS

The first and foremost impact of render-blocking JavaScript and CSS scripts on the website is that they slow down its loading speed. The site speed is a very important parameter that dictates how usable the website is and how well it performs in the SERPs. If the site loads slowly, it is more likely that visitors will abandon it for some other site; the ensuing bounce rate is a strong SEO signal that drives down the search engine rankings. According to searchenginejournal, more than the actual time spent in loading the site, the problem is with the perception of users of the time being taken for the website to load. Since browsers try to load everything, including the render-blocking scripts, at the same time, you should try to ensure that the site loads only those scripts required for the site to be usable at first, following which, the rest of the scripts can load.

Eliminating Render-Blocking JavaScript and CSS

Even before you try to remove the render-blocking scripts, you will need to identify the scripts that are responsible for the problem. Using Google’s PageSpeed Insights tool is the easiest way of doing this. You should make a list of the scripts that need attention and then decide whether you want to resolve the problem manually or use a plug-in, specially designed for that purpose.

However, if you implement certain best practices for reducing the number of render-blocking scripts on your website during the development stage, you will not only make life easier for yourself but also ensure affordable SEO. Some of the common tactics include minifying the JavaScript and CSS by removing unnecessary white space and comments in the code, concatenating JavaScript, and CSS files to reduce their overall number, as well as using asynchronous loading to defer the Java Script loading.

As simple as the methods may sound, manually removing the scripts can be tiresome because of the sheer number of JavaScript and CSS files that come with each of the front-facing plugins. While WordPress offers a combined filter for all front-end facing scripts using which, you can identify all incoming JavaScript or CSS files; it is much easier to employ a plug-in rather than doing it from scratch.

Top Plugins for Reducing the Number of Render-Blocking JavaScript and CSS Scripts

WP Rocket

WP Rocket acts as a very good tool for optimizing websites because it is extremely versatile and can handle a variety of tasks like minifying of CSS and JavaScript, deferring remote JavaScript requests, lazy loading images, and more. While the easy setup process is a big plus, the plug-in will require you to adjust to a different WordPress dashboard experience than what most developers are used to. Even though WP rocket comes with a few free extras, the base pricing for one website starts at $49 per annum.

Autoptimize

Built specifically to address issues identified by tools like PageSpeed insights, Autoptimize enables users to configure the plug-in with a new menu in the WordPress dashboard. While Autoptimize performs all the basic tasks like caching and minifying scripts, it can also optimize and convert images into the WebP file format. Though configuring it may be a little difficult, the free plug-in is rated very highly for its functionality. A $165 custom configuration plan and a $667 expert plug-in-configuration plan with a professional website review are also available.

JCH Optimize

JCH Optimize offers a set of unique tools for boosting your page loading speeds. For example, it can reduce the page sizes and the number of HTTP requests needed to load web pages that have the effect of decreasing the load on the server and lower the requirement of bandwidth.

The Sprite Generator that converts background images into sprites so that browser loading requires fewer HTTP requests. While users will like its advanced functionality, the steep learning curve can be a pain point even though adequate support documentation is available to ensure error-free installation. You can start off using the free version; however, if you want to access the advanced features, you will need to subscribe to one of the plans starting at $29 for six months.

Remove unused CSS JS files in WordPress

There are 4 fundamental capacities when you need to evacuate unused CSS JS records in WordPress front-end:

  • wp_deregister_script($handle)
  • wp_dequeue_script($handle)
  • wp_deregister_style($handle)
  • wp_dequeue_style($handle)

// expel content handles we needn’t bother with, each with their own conditions

add_action(‘wp_print_scripts’, ‘wra_filter_scripts’, 100000);

add_action(‘wp_print_footer_scripts’, ‘wra_filter_scripts’, 100000);

function wra_filter_scripts(){

#wp_deregister_script($handle);

#wp_dequeue_script($handle);

wp_deregister_script(‘bbpress-editor’);

wp_dequeue_script(‘bbpress-editor’);

// Device Pixels support

// this improves the goals of Gravatars and wordpress.com transfers on hi-res and zoomed programs. We just have Gravatars so we ought to be alright without it.

wp_deregister_script(‘devicepx’);

wp_dequeue_script(‘devicepx’);

if( !is_singular( ‘docs’ ) ){

wp_deregister_script(‘toc-front’);

wp_dequeue_script (‘toc-front’);

}

if( !is_singular( array(‘docs’, ‘post’ ) ) ){

wp_deregister_script (‘codebox’);

wp_dequeue_script (‘codebox’);

}

}

// remove styles we don’t need

add_action (‘wp_print_styles’, ‘wra_filter_styles’, 100000);

add_action (‘wp_print_footer_scripts’, ‘wra_filter_styles’, 100000);

function wra_filter_styles(){

#wp_deregister_style($handle);

#wp_dequeue_style($handle);

//no more bbpress styles.

wp_deregister_style(‘bbp-default’);

wp_dequeue_style(‘bbp-default’);

// download monitor is not used in the front-end.

wp_deregister_style(‘wp_dlmp_styles’);

wp_dequeue_style(‘wp_dlmp_styles’);

if( !is_singular( ‘docs’ ) ){

// the list of contents plug-in is being utilized on documentation pages as they were

wp_deregister_style(‘toc-screen’);

wp_dequeue_style(‘toc-screen’);

}

// this should not be like this. Need to look into it.

wp_deregister_style(‘wppb_stylesheet’);

wp_dequeue_style(‘wppb_stylesheet’);

}

if( !is_singular( array(‘docs’, ‘post’ ) ) ){

wp_deregister_style(‘codebox’);

wp_dequeue_style(‘codebox’);

}

}

The truly cool piece of doing it like this is you can utilize WordPress restrictive labels to focus on a specific page or a whole custom post type. This gives us the adaptability we have to stack our CSS/JS records precisely where they are required.

Conclusion 

Ensuring that your website loads quickly is essential to be user-friendly and prevent a high bounce rate from sinking your SEO performance. While there can be many reasons why your WordPress site may not be loading quickly, a common reason is the unnecessary queuing of render-blocking JavaScript and CSS scripts arising from the use of plugins.

To tackle the problems that even clean code writing cannot deal with, it can be helpful to use one of the specialized plug-in designed to reduce the number of JavaScript and CSS scripts as well as ensure that unnecessary queue formation of scripts does not delay the site loading.

About the author 

Imran Uddin


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}