How do I defer or async this WordPress javascript snippet to load lastly for faster page load times?

Cover Image for How do I defer or async this WordPress javascript snippet to load lastly for faster page load times?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

🚀 Boost your WordPress Page Load Times with Defer and Async JavaScript Snippets

Are you desperately searching for ways to make your WordPress website load faster? Look no further! We've got the solution for you – defer and async JavaScript snippets. 😎

Speed is crucial when it comes to retaining visitors, improving SEO, and enhancing user experience. Fortunately, by properly deferring or asyncing JavaScript, you can significantly boost your page load times and impress your audience. Let's dive right in and find out how you can accomplish this in your WordPress site.

Understanding the Problem

Before we provide easy solutions, let's understand the problem at hand. When a web page loads, JavaScript files can sometimes hinder the rendering process, leading to slower load times. The key is to prioritize what needs to load first and defer or async the rest.

In the example you provided, the PHP function add_dcsnt_scripts() enqueues two JavaScript files: jquery and dcsnt. To optimize this and achieve faster page load times, we need to make some adjustments.

Solution 1: Defer JavaScript Execution

Defering JavaScript allows the HTML to be parsed while the script is being fetched. This way, rendering won't be blocked, resulting in faster page loading. To defer a JavaScript file, modify your PHP code as follows:

function add_dcsnt_scripts() {
    wp_enqueue_script( 'jquery', '', array(), false, true );
    wp_enqueue_script( 'dcsnt', dc_jqsocialtabs::get_plugin_directory() . '/js/jquery.social.media.tabs.1.7.1.min.js', array('jquery'), false, true );
}

Notice the addition of the false, true arguments at the end of wp_enqueue_script() calls. This tells WordPress to load the scripts in the footer, allowing the HTML to load prior to executing the JavaScript code.

Solution 2: Async JavaScript Loading

If deferring is not possible due to specific dependencies, fear not! You can still improve your page load times by asyncing the JavaScript files. Asyncing allows the script to be fetched while the HTML is parsed, but its execution doesn't block rendering.

To async the JavaScript files, revise your PHP code as follows:

function add_dcsnt_scripts() {
    wp_enqueue_script( 'jquery', '', array(), false, true );
    wp_enqueue_script( 'dcsnt', dc_jqsocialtabs::get_plugin_directory() . '/js/jquery.social.media.tabs.1.7.1.min.js', array('jquery'), false, false );
}

Here, we replaced the true with false in the last argument of wp_enqueue_script('dcsnt'). This ensures that the script is fetched asynchronously.

Ideal Solution: Combination of Defer and Async

For the optimal solution, consider combining defer and async attributes. By using both, you can fully leverage the benefits of faster page load times. Here's a modified version of your PHP code:

function add_dcsnt_scripts() {
    wp_enqueue_script( 'jquery', '', array(), false, true );
    wp_enqueue_script( 'dcsnt', dc_jqsocialtabs::get_plugin_directory() . '/js/jquery.social.media.tabs.1.7.1.min.js', array('jquery'), false, true );
}

With this implementation, jquery will be deferred, and dcsnt will be both deferred and fetched asynchronously. This combination will help you achieve the best of both worlds.

👉 Take Action and Speed Up Your WordPress Site

Now that you've learned the two main methods to defer and async JavaScript loading, it's time to put this knowledge into action. Apply these changes to your PHP files where you enqueue your JavaScript, and watch your page load times improve significantly!

Remember, speed matters! The faster your website loads, the better user experience you provide. Say goodbye to slow loading pages and hello to happier visitors.

If you have any additional questions or need further guidance, feel free to reach out. Happy optimizing! 🚀🔥💪


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello