When you set up a WordPress site, it is important to measure and optimize its performance. Google’s PageSpeed Insights is an excellent tool for measuring site load speed and pointing out areas for improvement. However, you may notice that your site’s loading speed is not as fast as expected when using WordPress out of the box. This is because the default settings in WordPress often load unnecessary JavaScript and CSS files.
Here, we will show you how to remove specific file loads to improve the loading speed of your WordPress site. If you have technical knowledge, following these steps can significantly enhance your site’s performance.
In this example, we will edit the following file to remove unnecessary files:
wp-content\themes\your-theme-directory\functions.php
Remove Unnecessary jQuery Scripts
WordPress loads jQuery by default, but if you don’t use jQuery outside the admin panel, you can disable it. Add the following code to your functions.php
file:
function delete_jquery() {
if (!is_admin()) {
wp_deregister_script('jquery');
}
}
add_action('init', 'delete_jquery');
Remove CSS from the Block Editor (Gutenberg)
The WordPress block editor (Gutenberg) may also load unnecessary CSS on the site’s front end. To prevent this from being loaded outside the admin panel, add the following code to functions.php
:
function dequeue_plugins_style() {
if (!is_admin()) {
wp_dequeue_style('wp-block-library');
}
}
add_action('wp_enqueue_scripts', 'dequeue_plugins_style', 9999);
Conditionally Load Scripts and Stylesheets for Contact Form 7
The Contact Form 7 plugin loads JavaScript and CSS files even on pages where the form is not present. Here’s how to load them only on specific pages:
Exclude Contact Form 7’s JS and CSS files on all pages except for those using the form. Add the following to functions.php
:
add_action('wp', function() {
if (is_page('contactform')) return;
add_filter('wpcf7_load_js', '__return_false');
add_filter('wpcf7_load_css', '__return_false');
});
*Replace “contactform” with the slug of the page where Contact Form 7 is used.
Measuring Site Performance on PageSpeed Insights for This Site (DAD UNION)
After applying these optimizations, let’s see how the scores improved in PageSpeed Insights. Here are the results for this site (DAD UNION) as of August 3, 2021:
PageSpeed Insights Mobile: 88 Points
PageSpeed Insights Desktop: 96 Points
Conclusion
Optimizing the performance of your WordPress site is crucial for providing a comfortable browsing experience for visitors and improving search engine rankings. By implementing the optimization techniques mentioned above, you can effectively improve your site’s loading speed and attract more visitors. While the technical tasks may seem daunting, taking it step by step will surely enhance your site’s quality.
*Use these techniques at your own risk.