NGINX Proxy_Redirect
August 17, 2023zoho-mail-with-linode
August 24, 2023“Defer parsing of JavaScript” is a technique used to optimize the loading speed of a website by delaying the execution of JavaScript code until after the initial content of the page has been loaded. This can help improve the perceived performance of your WordPress website, especially for users with slower connections or devices.
In WordPress, you can implement the “defer parsing of JavaScript” technique using various methods, including plugins and manual code changes. Here’s how you can do it:
- Using a Plugin:
One of the easiest ways to defer JavaScript in WordPress is by using a plugin. One popular plugin for this purpose is “Autoptimize.” Here’s how you can set it up:
- Install and activate the “Autoptimize” plugin from the WordPress plugin repository.
- Go to the plugin’s settings (Settings > Autoptimize).
- Check the “Optimize JavaScript Code” option.
- Check the “Also aggregate inline JS?” option if you have inline JavaScript that you want to optimize.
- Check the “Defer” option under “Extra” settings.
- Save the settings. This plugin will automatically defer the loading and execution of JavaScript files on your WordPress site.
- Manual Implementation:
If you prefer to make changes directly to your theme’s files, you can manually defer JavaScript using the following steps:
- Open your theme’s
functions.php
file. - Add the following code to defer JavaScript execution:
function defer_parsing_of_js($url) { if (is_admin()) return $url; // Don't defer on WordPress admin pages if (false === strpos($url, '.js')) return $url; // Only defer JavaScript files if (strpos($url, 'jquery.js')) return $url; // Exclude jQuery return str_replace(' src', ' defer src', $url); } add_filter('script_loader_tag', 'defer_parsing_of_js', 10);
This code will add thedefer
attribute to all JavaScript files except for jQuery and on WordPress admin pages.
- Using Themes or Page Builders:
Some WordPress themes or page builders might have built-in options to enable deferred parsing of JavaScript. Check the documentation or settings of your theme or page builder to see if this feature is available.
Remember that while deferring JavaScript can improve page loading speed, it may also have unintended consequences if not implemented correctly. Be sure to thoroughly test your website after making these changes to ensure that everything works as expected.
Additionally, always keep your WordPress plugins, themes, and core files up to date to maintain compatibility and security.