{"id":139678,"date":"2016-03-17T11:00:40","date_gmt":"2016-03-17T11:00:40","guid":{"rendered":"https:\/\/getflywheel.com\/?p=15983"},"modified":"2024-06-25T18:58:10","modified_gmt":"2024-06-25T23:58:10","slug":"how-to-load-javascript-in-wordpress","status":"publish","type":"resource","link":"https:\/\/wpengine.com\/resources\/how-to-load-javascript-in-wordpress\/","title":{"rendered":"How to Load JavaScript in WordPress"},"content":{"rendered":"\n<p>JavaScript is one of the most popular coding languages around. It\u2019s incredibly useful when building a website or application, and there are countless JavaScript libraries and frameworks to tap into, each readily available to speed up and streamline the development process.<\/p>\n\n\n\n<p>With WordPress, there are a lot of moving parts that come together to make the site work. Various plugins, themes, and the core all utilize their own code, built independently of each other. If developers all do things their own way, we will inevitably run into conflicts, causing the site, at best, not to function efficiently and, at worst, break altogether.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices<\/h2>\n\n\n\n<p>To help alleviate these issues in relation to JavaScript, there are some best practices to follow when implementing it in your projects. Here\u2019s how to best leverage JavaScript when building WordPress themes and plugins.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Load Your JavaScript the Correct Way<\/h3>\n\n\n\n<p>If you are building a static site with vanilla HTML\/CSS, you can simply use JavaScript by loading it straight onto the page or typing it right inline, such as the following:<\/p>\n\n\n<p>[javascript]<\/p>\n<p>&lt;script src=&#8221;http:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.5\/jquery.min.js&#8221;&gt;&lt;\/script&gt;<\/p>\n<p>&lt;script&gt;<br \/>\n$(document).ready(function() {<br \/>\n$(&#8220;#hoverTrig&#8221;).hover(function () {<br \/>\n$(&#8220;#subNav&#8221;).stop();<br \/>\n$(&#8220;#subNav&#8221;).slideToggle(400,function() {<br \/>\nif ($(&#8216;#subNav&#8217;).height() &lt; 210) {<br \/>\n$(&#8220;#subNav&#8221;).css({height:210})<br \/>\n};<br \/>\n});<br \/>\n});<br \/>\n});<br \/>\n&lt;\/script&gt;<\/p>\n<p>[\/javascript]<\/p>\n\n\n\n<p>In WordPress, however, there is a different way to do it. To ensure that the same assets aren\u2019t being loaded multiple times and that libraries and files are loaded in the correct order, WordPress gives us two functions to use. To register, or essentially make WordPress aware of the script\u2019s existence, use <code>wp_register_script()<\/code>. To load a script onto a page, use <code>wp_enqueue_script()<\/code>.<\/p>\n\n\n<p>[javascript]<\/p>\n<p>wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );<\/p>\n<p>wp_register_script( $handle, $src, $deps, $ver, $in_footer );<\/p>\n<p>[\/javascript]<\/p>\n\n\n\n<p>The parameters for each are the same. Let\u2019s walk through them quickly.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>$handle<\/code> &#8211; This is the name of the script and should be unique. It\u2019s not necessarily the filename, although I find it helpful to sync up the handle and filename for maintainability.<\/li>\n\n\n\n<li><code>$src <\/code>&#8211; The path and filename where the script is located.<\/li>\n\n\n\n<li><code>$deps<\/code> &#8211; The handles for any scripts this file is dependant on. It should be an array and is optional.<\/li>\n\n\n\n<li><code>$ver<\/code> &#8211; The version number of the script.<\/li>\n\n\n\n<li><code>$in_footer<\/code> &#8211; This is a true\/false value, determining if the script should be loaded in the \u201cfooter\u201d before the closing <code>&lt;\/body&gt;<\/code> tag of the page (true) or before the closing <code>&lt;\/head&gt;<\/code> tag (false).<\/li>\n<\/ul>\n\n\n\n<p>Check out <a href=\"https:\/\/developer.wordpress.org\/reference\/\" target=\"_blank\" rel=\"noopener noreferrer\">the WordPress Code Reference<\/a> for more information on the parameters for <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_register_script\/\" target=\"_blank\" rel=\"noopener noreferrer\">wp_register_script<\/a> and <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_enqueue_script\/\" target=\"_blank\" rel=\"noopener noreferrer\">wp_enqueue_script<\/a>, and to see how the functions interact together.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using a Hook to Register and Enqueue<\/h3>\n\n\n\n<p>To ensure your scripts are ready and loaded on the front-end of your site, you\u2019ll also want to use the <code>wp_enqueue_scripts<\/code> hook. This hook is fired when WordPress loads styles and scripts. By using it, you are making sure the scripts you want included are considered anytime WordPress loads scripts or styles. <\/p>\n\n\n\n<p>When you use this hook, conditional logic can be incorporated to specify which pages the script should or should not be loaded on. In the following example, the <code>yourtheme_event<\/code> script is only being loaded on pages using the event.php template while the <code>yourtheme_custom<\/code> script is being loaded on every page.<\/p>\n\n\n<p>[php]<\/p>\n<p>function yourtheme_load_scripts() {<\/p>\n<p>if ( is_page_template(&#8216;event.php&#8217;) ) {<\/p>\n<p>wp_enqueue_script( &#8216;yourtheme_event&#8217;, get_template_directory_uri() . &#8216;\/js\/event.js&#8217;, &#8216;yourtheme_custom&#8217; );<\/p>\n<p>}<\/p>\n<p>wp_enqueue_script( &#8216;yourtheme_custom&#8217;, get_template_directory_uri() . &#8216;\/js\/custom.js&#8217;);<\/p>\n<p>}<\/p>\n<p>add_action( &#8216;wp_enqueue_scripts&#8217;, &#8216;yourtheme_load_scripts&#8217; );<\/p>\n<p>[\/php]<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Keeping Things Unique<\/h3>\n\n\n\n<p>Another important consideration when writing your own JavaScript for use in WordPress is to make your function names notably unique. If you name a function <code>loadEvent()<\/code> and another plugin also has a <code>loadEvent()<\/code> function, you\u2019ll have some issues. <\/p>\n\n\n\n<p>So, just like in a classroom with two students each named Sally, you\u2019ll want to make sure there\u2019s a way to differentiate between the two. You can either use a common prefix on functions that are unique, something like <code>yourtheme_<\/code>, or you can nest your function inside another object and avoid exposing it to the global window object.<\/p>\n\n\n<p>[php]<\/p>\n<p>var yourTheme = {<\/p>\n<p>var loadEvent = function() {<\/p>\n<p>\/\/ do something<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\/\/ call the function<\/p>\n<p>yourTheme.loadEvent();<\/p>\n<p>[\/php]<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Let\u2019s Talk About jQuery<\/h3>\n\n\n\n<p>As we wrap up, we want to make a quick note about jQuery. For a long time, many developers have struggled with implementing existing jQuery plugins in custom themes. WordPress comes with jQuery loaded out-of-the-box, so why weren\u2019t the jQuery plugins working? <\/p>\n\n\n\n<p>Well, WordPress loads <a rel=\"noopener noreferrer\" href=\"https:\/\/api.jquery.com\/jquery.noconflict\/\" target=\"_blank\">jQuery in no conflict mode<\/a>, meaning that using the widely adopted <code>$<\/code> as an alias for jQuery doesn\u2019t work in WordPress. digwp.com has <a rel=\"noopener noreferrer\" href=\"https:\/\/digwp.com\/2011\/09\/using-instead-of-jquery-in-wordpress\/\" target=\"_blank\">a nice article<\/a> on some options for how to work around this issue.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript is one of the most popular coding languages around. It\u2019s incredibly useful when building a website or application, and there are countless JavaScript libraries and frameworks to tap into, each readily available to speed up and streamline the development process. With WordPress, there are a lot of moving parts that come together to make<span class=\"tile__ellipses\">&hellip;<\/span><span class=\"tile__ellipses--animated\"><\/span><\/p>\n","protected":false},"author":297,"featured_media":16112,"template":"","resource-topic":[],"resource-role":[],"resource-type":[916],"class_list":["post-139678","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>How to Load JavaScript in WordPress | WP Engine<\/title>\n<meta name=\"description\" content=\"Learn the best practices for loading JavaScript in WordPress, including how to use certain codes to ensure website compatibility.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/wpengine.com\/resources\/how-to-load-javascript-in-wordpress\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Load JavaScript in WordPress | WP Engine\" \/>\n<meta property=\"og:description\" content=\"Learn the best practices for loading JavaScript in WordPress, including how to use certain codes to ensure website compatibility.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/wpengine.com\/resources\/how-to-load-javascript-in-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=\"2024-06-25T23:58:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/wpengine.com\/resources\/wp-content\/uploads\/2024\/05\/WPE-IMG-Thumbnail-1200x630-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\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\/resources\/how-to-load-javascript-in-wordpress\/\",\"url\":\"https:\/\/wpengine.com\/resources\/how-to-load-javascript-in-wordpress\/\",\"name\":\"How to Load JavaScript in WordPress | WP Engine\",\"isPartOf\":{\"@id\":\"https:\/\/wpengine.com\/resources\/#website\"},\"datePublished\":\"2016-03-17T11:00:40+00:00\",\"dateModified\":\"2024-06-25T23:58:10+00:00\",\"description\":\"Learn the best practices for loading JavaScript in WordPress, including how to use certain codes to ensure website compatibility.\",\"breadcrumb\":{\"@id\":\"https:\/\/wpengine.com\/resources\/how-to-load-javascript-in-wordpress\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/wpengine.com\/resources\/how-to-load-javascript-in-wordpress\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/wpengine.com\/resources\/how-to-load-javascript-in-wordpress\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/wpengine.com\/resources\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Resources\",\"item\":\"https:\/\/wpengine.com\/resources\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How to Load JavaScript in WordPress\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/wpengine.com\/resources\/#website\",\"url\":\"https:\/\/wpengine.com\/resources\/\",\"name\":\"WP Engine\",\"description\":\"Managed Hosting for WordPress\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/wpengine.com\/resources\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/wpengine.com\/resources\/#\/schema\/person\/3a22232b01de39dcf588fb8e421c0521\",\"name\":\"Erin Myers\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/wpengine.com\/resources\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cd881e115bc28c81642ec61752db9981ece9ee8b4c81498a9b6276b9cdcaf5e6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cd881e115bc28c81642ec61752db9981ece9ee8b4c81498a9b6276b9cdcaf5e6?s=96&d=mm&r=g\",\"caption\":\"Erin Myers\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Load JavaScript in WordPress | WP Engine","description":"Learn the best practices for loading JavaScript in WordPress, including how to use certain codes to ensure website compatibility.","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:\/\/wpengine.com\/resources\/how-to-load-javascript-in-wordpress\/","og_locale":"en_US","og_type":"article","og_title":"How to Load JavaScript in WordPress | WP Engine","og_description":"Learn the best practices for loading JavaScript in WordPress, including how to use certain codes to ensure website compatibility.","og_url":"https:\/\/wpengine.com\/resources\/how-to-load-javascript-in-wordpress\/","og_site_name":"WP Engine","article_publisher":"https:\/\/www.facebook.com\/wpengine","article_modified_time":"2024-06-25T23:58:10+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/wpengine.com\/resources\/wp-content\/uploads\/2024\/05\/WPE-IMG-Thumbnail-1200x630-1.jpg","type":"image\/jpeg"}],"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\/resources\/how-to-load-javascript-in-wordpress\/","url":"https:\/\/wpengine.com\/resources\/how-to-load-javascript-in-wordpress\/","name":"How to Load JavaScript in WordPress | WP Engine","isPartOf":{"@id":"https:\/\/wpengine.com\/resources\/#website"},"datePublished":"2016-03-17T11:00:40+00:00","dateModified":"2024-06-25T23:58:10+00:00","description":"Learn the best practices for loading JavaScript in WordPress, including how to use certain codes to ensure website compatibility.","breadcrumb":{"@id":"https:\/\/wpengine.com\/resources\/how-to-load-javascript-in-wordpress\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/wpengine.com\/resources\/how-to-load-javascript-in-wordpress\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/wpengine.com\/resources\/how-to-load-javascript-in-wordpress\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/wpengine.com\/resources\/"},{"@type":"ListItem","position":2,"name":"Resources","item":"https:\/\/wpengine.com\/resources\/"},{"@type":"ListItem","position":3,"name":"How to Load JavaScript in WordPress"}]},{"@type":"WebSite","@id":"https:\/\/wpengine.com\/resources\/#website","url":"https:\/\/wpengine.com\/resources\/","name":"WP Engine","description":"Managed Hosting for WordPress","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/wpengine.com\/resources\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/wpengine.com\/resources\/#\/schema\/person\/3a22232b01de39dcf588fb8e421c0521","name":"Erin Myers","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/wpengine.com\/resources\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cd881e115bc28c81642ec61752db9981ece9ee8b4c81498a9b6276b9cdcaf5e6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cd881e115bc28c81642ec61752db9981ece9ee8b4c81498a9b6276b9cdcaf5e6?s=96&d=mm&r=g","caption":"Erin Myers"}}]}},"acf":[],"grid_image_url":"https:\/\/wpengine.com\/resources\/wp-content\/themes\/wpengine-breakthrough\/images\/fallback\/default-grid-resource.jpg","media-type":{"term_id":916,"name":"Article","slug":"article"},"role":false,"topic":false,"_links":{"self":[{"href":"https:\/\/wpengine.com\/resources\/wp-json\/wp\/v2\/resource\/139678","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpengine.com\/resources\/wp-json\/wp\/v2\/resource"}],"about":[{"href":"https:\/\/wpengine.com\/resources\/wp-json\/wp\/v2\/types\/resource"}],"author":[{"embeddable":true,"href":"https:\/\/wpengine.com\/resources\/wp-json\/wp\/v2\/users\/297"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpengine.com\/resources\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/wpengine.com\/resources\/wp-json\/wp\/v2\/media?parent=139678"}],"wp:term":[{"taxonomy":"resource-topic","embeddable":true,"href":"https:\/\/wpengine.com\/resources\/wp-json\/wp\/v2\/resource-topic?post=139678"},{"taxonomy":"resource-role","embeddable":true,"href":"https:\/\/wpengine.com\/resources\/wp-json\/wp\/v2\/resource-role?post=139678"},{"taxonomy":"resource-type","embeddable":true,"href":"https:\/\/wpengine.com\/resources\/wp-json\/wp\/v2\/resource-type?post=139678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}