{"id":96841,"date":"2019-12-03T09:39:21","date_gmt":"2019-12-03T15:39:21","guid":{"rendered":"https:\/\/wpengine.com\/?post_type=resource&#038;p=96841"},"modified":"2025-10-15T15:28:09","modified_gmt":"2025-10-15T20:28:09","slug":"defer-parsing-javascript-wordpress","status":"publish","type":"resource","link":"https:\/\/wpengine.com\/case-studies\/resources\/defer-parsing-javascript-wordpress\/","title":{"rendered":"How To Defer Parsing of JavaScript in WordPress"},"content":{"rendered":"\n<p>When you use a site performance testing tool such as <a href=\"https:\/\/gtmetrix.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">GTmetrix<\/a>, <a href=\"https:\/\/developers.google.com\/speed\/pagespeed\/insights\/\" target=\"_blank\" rel=\"noreferrer noopener\">Google PageSpeed Insights<\/a>, or <a href=\"https:\/\/wpengine.com\/speed-tool\/\" target=\"_blank\" rel=\"noreferrer noopener\">WP Engine Speed Tool<\/a> it might suggest you \u201cdefer the parsing of JavaScript.\u201d While common, this recommendation can also be confusing.&nbsp;<\/p>\n\n\n\n<p>In short, browsers render and download JavaScript from the server before loading any other site content. This can interrupt page load times and negatively impact site speed. Fortunately, you can defer this so the browser can load the site content first, without waiting for the scripts to finish downloading.&nbsp;<\/p>\n\n\n\n<p>One way to defer JavaScript parsing is with WP Engine\u2019s <a href=\"https:\/\/wpengine.com\/ca\/page-speed-boost\/\" target=\"_blank\" rel=\"noreferrer noopener\">Page Speed Boost<\/a>. In addition to deferring JavaScript parsing, Page Speed Boost offers more than 30 other front-end optimizations to help ensure pages are served lightning fast and your users aren\u2019t kept waiting.<\/p>\n\n\n\n<p>In this post, we\u2019ll explain the different methods you can use to defer JavaScript parsing. We\u2019ll provide step-by-step instructions and break down the pros and cons of each method. Let\u2019s get started!<\/p>\n\n\n\n\n\n<h2 class=\"wp-block-heading\">Before you get started<\/h2>\n\n\n\n<p>Before we get into the different methods you can use for deferring parsing of JavaScript, it\u2019s important to first <a href=\"https:\/\/wpengine.com\/support\/restore\/\" target=\"_blank\" rel=\"noreferrer noopener\">backup your site<\/a> in case the worst happens and you lose your site. Then, you\u2019ll be safe to proceed with either of the methods we\u2019ll discuss.&nbsp;<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method 1: Deferring parsing of JavaScript via <em>functions.php<\/em><\/h2>\n\n\n\n<p>When you need to manually defer JavaScript files in WordPress, the recommended approach is to use a custom function that leverages the core WordPress API. This method is for users comfortable with editing theme files and gives you precise control over which scripts are deferred.<\/p>\n\n\n\n<p>To defer a script, you&#8217;ll use the <code>wp_enqueue_script()<\/code> or <code>wp_register_script()<\/code><strong> <\/strong>functions and include a <code>strategy<\/code> argument. This tells WordPress to automatically add the <code>defer<\/code> or <code>async<\/code> attribute to the script&#8217;s HTML tag. Here is an example of how to enqueue a custom script with the <code>defer<\/code> attribute. You can add this code to a <a href=\"https:\/\/wpengine.com\/resources\/how-to-create-a-wordpress-child-theme\/\">child theme&#8217;s<\/a> <code>functions.php<\/code> file or to a <a href=\"https:\/\/wpengine.com\/resources\/how-to-create-and-install-must-use-plugins-in-wordpress\/\">mu-plugin<\/a>. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function my_custom_scripts() {\n    wp_enqueue_script(\n        'my-custom-script',\n        get_template_directory_uri() . '\/js\/my-custom-script.js',\n        array(),\n        '1.0',\n        array(\n            'in_footer' => true,\n            'strategy'  => 'defer',\n        )\n    );\n}\nadd_action( 'wp_enqueue_scripts', 'my_custom_scripts' );\n<\/code><\/pre>\n\n\n\n<p>In this code snippet, the <code>strategy <\/code>key with a value of <code>defer<\/code> ensures the script will be deferred. The <code>in_footer<\/code> key moves the script to the footer (just before the closing <code>&lt;\/body><\/code> tag), ensuring the browser renders all the visible content first. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>defer vs. async<\/strong><\/h4>\n\n\n\n<p>WordPress supports both <code>defer<\/code> and <code>async<\/code> attributes. It&#8217;s important to choose the right one for your specific script:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>defer<\/strong>: Use this for scripts that depend on other scripts or the page&#8217;s HTML structure. A deferred script downloads in the background and executes only after the HTML document is fully parsed, in the exact order they were enqueued.<\/li>\n\n\n\n<li><strong>async<\/strong>: Use this for scripts that are self-contained and don&#8217;t rely on other scripts or the DOM. An asynchronous script downloads and executes as soon as it&#8217;s ready, without waiting for the HTML to parse and without a guaranteed execution order.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Testing your changes<\/strong><\/h4>\n\n\n\n<p>After implementing this code, test your website to ensure everything functions correctly. Deferring scripts can sometimes lead to unexpected behavior if not handled properly. Use a site speed tool like <a href=\"https:\/\/gtmetrix.com\/\">GTmetrix<\/a> or <a href=\"https:\/\/pagespeed.web.dev\/\">Google PageSpeed Insights<\/a> to confirm that the changes have improved your site&#8217;s performance.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method 2: Defer parsing of JavaScript with a plugin<\/h2>\n\n\n\n<p>Even if you\u2019re comfortable with manually editing code, using a comprehensive performance plugin is often the safest and most effective way to defer JavaScript.&nbsp;<\/p>\n\n\n\n<p><a href=\"https:\/\/nitropack.io\/\">NitroPack<\/a> is an all-in-one performance optimization plugin that automatically defers JavaScript parsing to improve page load speed. Instead of just adding a defer or async attribute, NitroPack&#8217;s approach is to delay the loading of non-critical scripts until after the main page content has loaded or until a user interacts with the page. This is a highly effective technique for passing performance tests and significantly improving perceived load times.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>How to defer JavaScript using NitroPack<\/strong><\/h4>\n\n\n\n<p>The first step is to install and activate the plugin. From your WordPress dashboard, navigate to <strong>Plugins &gt; Add Plugin<\/strong>, search for &#8220;NitroPack,&#8221; and follow the on-screen instructions to install and activate it. You\u2019ll also need to connect your website to your <a href=\"https:\/\/support.nitropack.io\/en\/articles\/8390231-how-to-create-a-nitropack-account\">NitroPack account<\/a>.<\/p>\n\n\n\n<p>Next, click Nitropack in your WordPress admin to choose your optimization mode. NitroPack offers four pre-built optimization modes: &#8220;Standard,&#8221; \u201cMedium,\u201d &#8220;Strong,&#8221; and &#8220;Ludicrous.&#8221; Each mode has <a href=\"https:\/\/support.nitropack.io\/en\/articles\/8390314-which-settings-are-enabled-in-each-optimization-mode\">different preconfigured settings<\/a>. To defer JavaScript, select \u201cMedium\u201d or above. This will <a href=\"https:\/\/support.nitropack.io\/en\/articles\/8390467-how-to-deal-with-render-blocking-resources-in-nitropack\">remove render-blocking resources<\/a>, which include both JavaScript and CSS.&nbsp;<\/p>\n\n\n\n<p>If you notice that a specific third-party script is still impacting your performance scores, you can manually add it to NitroPack&#8217;s <a href=\"https:\/\/support.nitropack.io\/en\/articles\/8390304-delayed-scripts\">delayed scripts<\/a> list. This allows for a more targeted deferral.&nbsp;<\/p>\n\n\n\n<p>NitroPack simplifies the entire process of deferring JavaScript. It handles not just script deferral, but also caching, minification, image optimization, and more, all of which contribute to a faster, more performant website.\u00a0<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Speed up your experience with WP Engine<\/h2>\n\n\n\n<p>JavaScript is an essential component of your WordPress site. However, it can also negatively impact the performance of your site and slow page load times. There are two methods you can use to defer parsing of JavaScript \u2013 manually via your <em>functions.php<\/em> file or a plugin. Either method will help improve load times and overall user experience.<\/p>\n\n\n\n<p>Here at WP Engine, we understand the pivotal importance of following <a href=\"https:\/\/developer.wordpress.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">development best practices<\/a> to provide an incredible user experience for your customers. Learn more about our fully managed <a href=\"https:\/\/wpengine.com\/wordpress-hosting\/\" target=\"_blank\" rel=\"noreferrer noopener\">WordPress hosting<\/a> and find the right <a href=\"https:\/\/wpengine.com\/plans\/\" target=\"_blank\" rel=\"noreferrer noopener\">plan<\/a> for you to see the many ways can help you speed up your experience and site performance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you use a site performance testing tool such as GTmetrix, Google PageSpeed Insights, or WP Engine Speed Tool it might suggest you \u201cdefer the parsing of JavaScript.\u201d While common, this recommendation can also be confusing.&nbsp; In short, browsers render and download JavaScript from the server before loading any other site content. This can interrupt<span class=\"tile__ellipses\">&hellip;<\/span><span class=\"tile__ellipses--animated\"><\/span><\/p>\n","protected":false},"author":1,"featured_media":148204,"template":"","resource-topic":[912],"resource-role":[895,896,906,897,899],"resource-type":[916],"class_list":["post-96841","resource","type-resource","status-publish","has-post-thumbnail","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Defer Parsing Of JavaScript In WordPress<\/title>\n<meta name=\"description\" content=\"Deferring the parsing of JavaScript can help speed up your WordPress site. Find out how today with WP Engine!\" \/>\n<meta name=\"robots\" content=\"noindex, follow\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Defer Parsing Of JavaScript In WordPress\" \/>\n<meta property=\"og:description\" content=\"Deferring the parsing of JavaScript can help speed up your WordPress site. Find out how today with WP Engine!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/wpengine.com\/case-studies\/resources\/defer-parsing-javascript-wordpress\/\" \/>\n<meta property=\"og:site_name\" content=\"WP Engine\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/wpengine\" \/>\n<meta property=\"article:modified_time\" content=\"2025-10-15T20:28:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2019\/12\/js-header.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"500\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@wpengine\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/defer-parsing-javascript-wordpress\/\",\"url\":\"https:\/\/wpengine.com\/case-studies\/resources\/defer-parsing-javascript-wordpress\/\",\"name\":\"Defer Parsing Of JavaScript In WordPress\",\"isPartOf\":{\"@id\":\"https:\/\/wpengine.com\/case-studies\/#website\"},\"datePublished\":\"2019-12-03T15:39:21+00:00\",\"dateModified\":\"2025-10-15T20:28:09+00:00\",\"description\":\"Deferring the parsing of JavaScript can help speed up your WordPress site. Find out how today with WP Engine!\",\"breadcrumb\":{\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/defer-parsing-javascript-wordpress\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/wpengine.com\/case-studies\/resources\/defer-parsing-javascript-wordpress\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/defer-parsing-javascript-wordpress\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/wpengine.com\/case-studies\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Resources\",\"item\":\"https:\/\/wpengine.com\/case-studies\/resources\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How To Defer Parsing of JavaScript in WordPress\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#website\",\"url\":\"https:\/\/wpengine.com\/case-studies\/\",\"name\":\"WP Engine\",\"description\":\"Managed Hosting for WordPress\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/wpengine.com\/case-studies\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/f5301455463371a10d1fc290e9ad0085\",\"name\":\"WP Engine\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g\",\"caption\":\"WP Engine\"},\"sameAs\":[\"https:\/\/wpengine.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Defer Parsing Of JavaScript In WordPress","description":"Deferring the parsing of JavaScript can help speed up your WordPress site. Find out how today with WP Engine!","robots":{"index":"noindex","follow":"follow"},"og_locale":"en_US","og_type":"article","og_title":"Defer Parsing Of JavaScript In WordPress","og_description":"Deferring the parsing of JavaScript can help speed up your WordPress site. Find out how today with WP Engine!","og_url":"https:\/\/wpengine.com\/case-studies\/resources\/defer-parsing-javascript-wordpress\/","og_site_name":"WP Engine","article_publisher":"https:\/\/www.facebook.com\/wpengine","article_modified_time":"2025-10-15T20:28:09+00:00","og_image":[{"width":1100,"height":500,"url":"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2019\/12\/js-header.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_site":"@wpengine","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/wpengine.com\/case-studies\/resources\/defer-parsing-javascript-wordpress\/","url":"https:\/\/wpengine.com\/case-studies\/resources\/defer-parsing-javascript-wordpress\/","name":"Defer Parsing Of JavaScript In WordPress","isPartOf":{"@id":"https:\/\/wpengine.com\/case-studies\/#website"},"datePublished":"2019-12-03T15:39:21+00:00","dateModified":"2025-10-15T20:28:09+00:00","description":"Deferring the parsing of JavaScript can help speed up your WordPress site. Find out how today with WP Engine!","breadcrumb":{"@id":"https:\/\/wpengine.com\/case-studies\/resources\/defer-parsing-javascript-wordpress\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/wpengine.com\/case-studies\/resources\/defer-parsing-javascript-wordpress\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/wpengine.com\/case-studies\/resources\/defer-parsing-javascript-wordpress\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/wpengine.com\/case-studies\/"},{"@type":"ListItem","position":2,"name":"Resources","item":"https:\/\/wpengine.com\/case-studies\/resources\/"},{"@type":"ListItem","position":3,"name":"How To Defer Parsing of JavaScript in WordPress"}]},{"@type":"WebSite","@id":"https:\/\/wpengine.com\/case-studies\/#website","url":"https:\/\/wpengine.com\/case-studies\/","name":"WP Engine","description":"Managed Hosting for WordPress","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/wpengine.com\/case-studies\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/f5301455463371a10d1fc290e9ad0085","name":"WP Engine","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g","caption":"WP Engine"},"sameAs":["https:\/\/wpengine.com"]}]}},"acf":[],"grid_image_url":"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2019\/12\/js-grid.png","media-type":{"term_id":916,"name":"Article","slug":"article"},"role":"<strong>Roles:<\/strong> Agency, Developer, Entrepreneur, Freelancer, Site Owner","topic":"<strong>Topics:<\/strong> Performance","_links":{"self":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource\/96841","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource"}],"about":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/types\/resource"}],"author":[{"embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/users\/1"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/media\/148204"}],"wp:attachment":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/media?parent=96841"}],"wp:term":[{"taxonomy":"resource-topic","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-topic?post=96841"},{"taxonomy":"resource-role","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-role?post=96841"},{"taxonomy":"resource-type","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-type?post=96841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}