{"id":574,"date":"2019-05-14T02:45:03","date_gmt":"2019-05-14T02:45:03","guid":{"rendered":"http:\/\/www.projectimmerse.com\/blog\/?p=574"},"modified":"2020-06-16T02:58:54","modified_gmt":"2020-06-16T02:58:54","slug":"script-loader-tag-using-wordpress","status":"publish","type":"post","link":"https:\/\/www.projectimmerse.com\/blog\/script-loader-tag-using-wordpress\/","title":{"rendered":"Script Loader Tag using WordPress"},"content":{"rendered":"<p>This little tidbit I consider a golden nugget for those looking to optimize their website&#8217;s page speed. I ran into a situation recently (actually times) where I needed to defer some scripts down to the footer area. If you&#8217;ve been developing a while now, you know about render blocking scripts. Put simply, we want our DOM to load first before any scripts are executed.<\/p>\r\n<p><!--more--><\/p>\r\n<p>So there are two wordpress hooks you&#8217;re probably familiar with &#8211; they&#8217;re mentioned below.<\/p>\r\n<p>For CSS, we can utilize the functions below:<\/p>\r\n<pre><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nwp_enqueue_style();\r\nwp_register_style();\r\n<\/pre><\/pre>\r\n<p>And for scripts:<\/p>\r\n<pre><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nwp_enqueue_script\r\n<\/pre><\/pre>\r\n<p>That&#8217;s all good and dandy, but what about if we want to add script and\/or css properties? To add those extra properties we will need to utilize yet another WordPress function I didn&#8217;t even know existed.<\/p>\r\n<p>This may be useful when you&#8217;re looking to add extra properties\/attributes to your script tag. Some examples could be:<\/p>\r\n<h2><a title=\"HTML defer Attribute\" href=\"https:\/\/www.w3schools.com\/tags\/att_script_defer.asp\">defer<\/a><\/h2>\r\n<pre><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&amp;amp;lt;script src=&amp;amp;quot;defer-script.js&amp;amp;quot; defer&amp;amp;lt;\/script&amp;amp;gt;\r\n<\/pre><\/pre>\r\n<h2><a title=\"HTML async Attribute\" href=\"https:\/\/www.w3schools.com\/tags\/att_script_async.asp\">async<\/a><\/h2>\r\n<pre><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&amp;amp;lt;script src=&amp;amp;quot;async-script.js&amp;amp;quot; async&amp;amp;lt;\/script&amp;amp;gt;\r\n<\/pre><\/pre>\r\n<h2><a title=\"crossorigin property\" href=\"https:\/\/www.w3schools.com\/css\/css_icons.asp\">crossorigin<\/a><\/h2>\r\n<p>We see this used most often when calling files from a remote server ie. bootstrap, icons, libraries.<\/p>\r\n<pre><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\r\n&amp;amp;lt;link rel=&amp;amp;quot;stylesheet&amp;amp;quot; href=&amp;amp;quot;https:\/\/use.fontawesome.com\/releases\/v5.7.0\/css\/all.css&amp;amp;quot; integrity=&amp;amp;quot;sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ&amp;amp;quot; crossorigin=&amp;amp;quot;anonymous&amp;amp;quot;&amp;amp;gt;\r\n\r\n<\/pre><\/pre>\r\n<h2>script_loader_tag Usage<\/h2>\r\n<p>Alright now that we&#8217;ve got that squared away, we can dive into the function itself. Implementation quite similar to WordPress&#8217;s family of functions &#8211; first define the function. In our case, let&#8217;s call our function <strong>my_defer_scripts<\/strong><\/p>\r\n<p>Next, we want to define an array listing our scripts &#8211; let&#8217;s name it <strong>$defer_scripts<\/strong><\/p>\r\n<p>The return value is what&#8217;s most important &#8211; this is where we add the property. The working script below illustrates how straightforward this is.<\/p>\r\n<p>Finally, we pass our defined function onto WordPress&#8217; filter function and voila &#8211; we&#8217;ve got the defer property added to our script tag.<\/p>\r\n<pre><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\r\nfunction my_defer_scripts($tag, $handle, $source) {\r\n\r\n    $defer_scripts = array( 'some-script', 'another-script' );\r\n\r\nif(in_array($handle, $async_scripts)) {\r\n    return '&amp;amp;lt;script type=&amp;amp;quot;text\/javascript&amp;amp;quot; src=' . $src . ' defer&amp;amp;gt;&amp;amp;lt;\/script&amp;amp;gt;';\r\n}\r\n\r\n    return $tag;\r\n}\r\nadd_filter( 'script_loader_tag', 'my_defer_scripts', 10, 3 );\r\n\r\n<\/pre><\/pre>\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>This little tidbit I consider a golden nugget for those looking to optimize their website&#8217;s page speed. I ran into a situation recently (actually times) where I needed to defer some scripts down to the footer area. If you&#8217;ve been developing a while now, you know about render blocking scripts. Put simply, we want our &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.projectimmerse.com\/blog\/script-loader-tag-using-wordpress\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Script Loader Tag using WordPress&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1106,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[57],"tags":[],"class_list":["post-574","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Script Loader Tag using Wordpress - Project Immerse<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.projectimmerse.com\/blog\/script-loader-tag-using-wordpress\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Script Loader Tag using Wordpress - Project Immerse\" \/>\n<meta property=\"og:description\" content=\"This little tidbit I consider a golden nugget for those looking to optimize their website&#8217;s page speed. I ran into a situation recently (actually times) where I needed to defer some scripts down to the footer area. If you&#8217;ve been developing a while now, you know about render blocking scripts. Put simply, we want our &hellip; Continue reading &quot;Script Loader Tag using WordPress&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.projectimmerse.com\/blog\/script-loader-tag-using-wordpress\/\" \/>\n<meta property=\"og:site_name\" content=\"Project Immerse\" \/>\n<meta property=\"article:published_time\" content=\"2019-05-14T02:45:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-06-16T02:58:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2019\/05\/script-loader-tag-wordpress.png\" \/>\n\t<meta property=\"og:image:width\" content=\"750\" \/>\n\t<meta property=\"og:image:height\" content=\"750\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Alex Mercer\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Alex Mercer\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/script-loader-tag-using-wordpress\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/script-loader-tag-using-wordpress\\\/\"},\"author\":{\"name\":\"Alex Mercer\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#\\\/schema\\\/person\\\/c53f2864be524ee6fa08a7e4800dd1e5\"},\"headline\":\"Script Loader Tag using WordPress\",\"datePublished\":\"2019-05-14T02:45:03+00:00\",\"dateModified\":\"2020-06-16T02:58:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/script-loader-tag-using-wordpress\\\/\"},\"wordCount\":289,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/script-loader-tag-using-wordpress\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/05\\\/script-loader-tag-wordpress.png\",\"articleSection\":[\"Web Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/script-loader-tag-using-wordpress\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/script-loader-tag-using-wordpress\\\/\",\"url\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/script-loader-tag-using-wordpress\\\/\",\"name\":\"Script Loader Tag using Wordpress - Project Immerse\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/script-loader-tag-using-wordpress\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/script-loader-tag-using-wordpress\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/05\\\/script-loader-tag-wordpress.png\",\"datePublished\":\"2019-05-14T02:45:03+00:00\",\"dateModified\":\"2020-06-16T02:58:54+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#\\\/schema\\\/person\\\/c53f2864be524ee6fa08a7e4800dd1e5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/script-loader-tag-using-wordpress\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/script-loader-tag-using-wordpress\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/script-loader-tag-using-wordpress\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/05\\\/script-loader-tag-wordpress.png\",\"contentUrl\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/05\\\/script-loader-tag-wordpress.png\",\"width\":750,\"height\":750,\"caption\":\"Script Loader Tag - Wordpress\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/script-loader-tag-using-wordpress\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Script Loader Tag using WordPress\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/\",\"name\":\"Project Immerse\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#\\\/schema\\\/person\\\/c53f2864be524ee6fa08a7e4800dd1e5\",\"name\":\"Alex Mercer\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/alex-mercer-author-150x150.png\",\"url\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/alex-mercer-author-150x150.png\",\"contentUrl\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/alex-mercer-author-150x150.png\",\"caption\":\"Alex Mercer\"},\"description\":\"Alex Mercer is a writer and contributor at Project Immerse, covering web design, WordPress, web development, SEO, digital strategy, emerging technology, and the evolving world of the web. His work focuses on practical ideas, useful tools, and approachable guidance that help creators, developers, and business owners build better digital experiences. Alex writes with a hands-on, curiosity-driven approach, exploring both established web practices and new technologies shaping how websites are designed, built, and discovered.\",\"sameAs\":[\"https:\\\/\\\/www.projectimmerse.com\"],\"url\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/author\\\/projectimmerse\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Script Loader Tag using Wordpress - Project Immerse","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.projectimmerse.com\/blog\/script-loader-tag-using-wordpress\/","og_locale":"en_US","og_type":"article","og_title":"Script Loader Tag using Wordpress - Project Immerse","og_description":"This little tidbit I consider a golden nugget for those looking to optimize their website&#8217;s page speed. I ran into a situation recently (actually times) where I needed to defer some scripts down to the footer area. If you&#8217;ve been developing a while now, you know about render blocking scripts. Put simply, we want our &hellip; Continue reading \"Script Loader Tag using WordPress\"","og_url":"https:\/\/www.projectimmerse.com\/blog\/script-loader-tag-using-wordpress\/","og_site_name":"Project Immerse","article_published_time":"2019-05-14T02:45:03+00:00","article_modified_time":"2020-06-16T02:58:54+00:00","og_image":[{"width":750,"height":750,"url":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2019\/05\/script-loader-tag-wordpress.png","type":"image\/png"}],"author":"Alex Mercer","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Alex Mercer","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.projectimmerse.com\/blog\/script-loader-tag-using-wordpress\/#article","isPartOf":{"@id":"https:\/\/www.projectimmerse.com\/blog\/script-loader-tag-using-wordpress\/"},"author":{"name":"Alex Mercer","@id":"https:\/\/www.projectimmerse.com\/blog\/#\/schema\/person\/c53f2864be524ee6fa08a7e4800dd1e5"},"headline":"Script Loader Tag using WordPress","datePublished":"2019-05-14T02:45:03+00:00","dateModified":"2020-06-16T02:58:54+00:00","mainEntityOfPage":{"@id":"https:\/\/www.projectimmerse.com\/blog\/script-loader-tag-using-wordpress\/"},"wordCount":289,"commentCount":0,"image":{"@id":"https:\/\/www.projectimmerse.com\/blog\/script-loader-tag-using-wordpress\/#primaryimage"},"thumbnailUrl":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2019\/05\/script-loader-tag-wordpress.png","articleSection":["Web Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.projectimmerse.com\/blog\/script-loader-tag-using-wordpress\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.projectimmerse.com\/blog\/script-loader-tag-using-wordpress\/","url":"https:\/\/www.projectimmerse.com\/blog\/script-loader-tag-using-wordpress\/","name":"Script Loader Tag using Wordpress - Project Immerse","isPartOf":{"@id":"https:\/\/www.projectimmerse.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.projectimmerse.com\/blog\/script-loader-tag-using-wordpress\/#primaryimage"},"image":{"@id":"https:\/\/www.projectimmerse.com\/blog\/script-loader-tag-using-wordpress\/#primaryimage"},"thumbnailUrl":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2019\/05\/script-loader-tag-wordpress.png","datePublished":"2019-05-14T02:45:03+00:00","dateModified":"2020-06-16T02:58:54+00:00","author":{"@id":"https:\/\/www.projectimmerse.com\/blog\/#\/schema\/person\/c53f2864be524ee6fa08a7e4800dd1e5"},"breadcrumb":{"@id":"https:\/\/www.projectimmerse.com\/blog\/script-loader-tag-using-wordpress\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.projectimmerse.com\/blog\/script-loader-tag-using-wordpress\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.projectimmerse.com\/blog\/script-loader-tag-using-wordpress\/#primaryimage","url":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2019\/05\/script-loader-tag-wordpress.png","contentUrl":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2019\/05\/script-loader-tag-wordpress.png","width":750,"height":750,"caption":"Script Loader Tag - Wordpress"},{"@type":"BreadcrumbList","@id":"https:\/\/www.projectimmerse.com\/blog\/script-loader-tag-using-wordpress\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.projectimmerse.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Script Loader Tag using WordPress"}]},{"@type":"WebSite","@id":"https:\/\/www.projectimmerse.com\/blog\/#website","url":"https:\/\/www.projectimmerse.com\/blog\/","name":"Project Immerse","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.projectimmerse.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.projectimmerse.com\/blog\/#\/schema\/person\/c53f2864be524ee6fa08a7e4800dd1e5","name":"Alex Mercer","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2026\/09\/alex-mercer-author-150x150.png","url":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2026\/09\/alex-mercer-author-150x150.png","contentUrl":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2026\/09\/alex-mercer-author-150x150.png","caption":"Alex Mercer"},"description":"Alex Mercer is a writer and contributor at Project Immerse, covering web design, WordPress, web development, SEO, digital strategy, emerging technology, and the evolving world of the web. His work focuses on practical ideas, useful tools, and approachable guidance that help creators, developers, and business owners build better digital experiences. Alex writes with a hands-on, curiosity-driven approach, exploring both established web practices and new technologies shaping how websites are designed, built, and discovered.","sameAs":["https:\/\/www.projectimmerse.com"],"url":"https:\/\/www.projectimmerse.com\/blog\/author\/projectimmerse\/"}]}},"_links":{"self":[{"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/posts\/574","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/comments?post=574"}],"version-history":[{"count":25,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/posts\/574\/revisions"}],"predecessor-version":[{"id":604,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/posts\/574\/revisions\/604"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/media\/1106"}],"wp:attachment":[{"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/media?parent=574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/categories?post=574"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/tags?post=574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}