{"id":869,"date":"2016-06-19T21:26:04","date_gmt":"2016-06-19T21:26:04","guid":{"rendered":"http:\/\/ashton.codes\/blog\/?p=869"},"modified":"2019-08-12T18:02:15","modified_gmt":"2019-08-12T17:02:15","slug":"use-requirejs-wordpress-plugins-jquery-ui","status":"publish","type":"post","link":"https:\/\/ashton.codes\/use-requirejs-wordpress-plugins-jquery-ui\/","title":{"rendered":"Use RequireJS with WordPress (plugins) &#038; jQuery (UI)"},"content":{"rendered":"\n<p>I recently found myself really wanting to use RequireJS with WordPress, to manage the various JavaScript dependencies a client site had. Unfortunately, this was easier said than done.<\/p>\n\n\n\n<p>WordPress is historically not very compatible with RequireJS, as it provides jQuery and a myriad of other JavaScript files out of the box, which are difficult to shoehorn into your RequireJS configuration. There are loads of WordPress plugins out there which rely on jQuery being a global variable in the page.<\/p>\n\n\n\n<p>Also, any attempt at loading RequireJS into the page will often result in errors because many JavaScript modules first check if <code>require<\/code> is defined before they execute. This means that the very act of including RequireJS in the page will change how some code is executed, and cause you lots of headaches!<\/p>\n\n\n\n<p>We need <code>require<\/code> to be defined so that we can make lots of lovely <code>require<\/code> calls throughout our webpage, but we can&#8217;t actually define <code>require<\/code> until all of WordPress&#8217; native dependencies and plugin JS files have sorted themselves out.<\/p>\n\n\n\n<p>I managed to hack together a solution, and it seems to work rather well.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Edit your header<\/h2>\n\n\n\n<p>First of all, edit header.php (I put this just above the <code>wp_head()<\/code> call):<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; auto-links: false; title: ; notranslate\" title=\"\">\n&lt;script&gt;\nwindow.queueForRequire = &#x5B;];\n \nwindow.r = function (deps, callback) {\n    window.queueForRequire.push({\n        deps:     deps,\n        callback: callback\n    });\n};\n&lt;\/script&gt;\n<\/pre><\/div>\n\n\n<p>We&#8217;re going to store any <code>require()<\/code> calls in a queue: <code>queueForRequire<\/code>.<\/p>\n\n\n\n<p>Then require your modules as normal. Well, as semi-normal: as mentioned previously, many JavaScript files will check if <code>require<\/code> is defined and misbehave if it is, so instead of mocking <code>require<\/code>, I&#8217;ve made a custom function <code>r<\/code> which we&#8217;ll use instead.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; auto-links: false; title: ; notranslate\" title=\"\">\n&lt;script&gt;\nr(&#x5B;&#039;subscribe-cta&#039;], function (subscribe) {\n    console.log(&#039;This is my callback&#039;);\n});\n \n&lt;?php if (is_home() &amp;&amp; apply_filters(&#039;require_slider&#039;, $shouldLoadSlider)) : ?&gt;\nr(&#x5B;&#039;slider&#039;]);\n&lt;?php endif; ?&gt;\n \n&lt;?php if (is_archive() || is_home()) : ?&gt;\nr(&#x5B;&#039;infinite-scroll&#039;]);\n&lt;?php endif; ?&gt;\n&lt;\/script&gt;\n<\/pre><\/div>\n\n\n<p>At this stage, all we&#8217;re doing is adding <code>require<\/code> calls to a queue &#8211; nothing is being downloaded just yet.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Edit your footer<\/h2>\n\n\n\n<p>Now edit footer.php (put this right before the closing <code>&lt;\/body><\/code> tag):<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; auto-links: false; title: ; notranslate\" title=\"\">\n&lt;script src=&quot;https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/require.js\/2.2.0\/require.min.js&quot;&gt;&lt;\/script&gt;\n&lt;script&gt;\n    require.config({\n        baseUrl: &#039;\/wordpress\/wp-content\/themes\/tc-magazine-core\/js&#039;,\n        paths: {\n            &#039;readmore&#039;: &#039;https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/Readmore.js\/2.1.0\/readmore.min&#039;\n        },\n        waitSeconds: 15\n    });\n \n    if (typeof jQuery === &#039;function&#039;) {\n        define(&#039;jquery&#039;, function () { return jQuery; });\n    }\n \n    for (var i = 0; i &lt; window.queueForRequire.length; i++) {\n        require(window.queueForRequire&#x5B;i].deps, window.queueForRequire&#x5B;i].callback);\n    }\n&lt;\/script&gt;\n<\/pre><\/div>\n\n\n<p>Only at the very end of the document do we go &#8220;now I&#8217;m ready to load RequireJS. WordPress has done its thing, so it should be safe for us to do ours now&#8221;.<\/p>\n\n\n\n<p>So, this is pretty standard stuff: we&#8217;re downloading the RequireJS library from a CDN and initialising it with a config. jQuery is a strange special case handled in lines 11-13: we want our modules to be able to pull in jQuery as a module, but know jQuery is probably a global already, so here we&#8217;re creating a jQuery AMD module on the fly.<\/p>\n\n\n\n<p>The magic happens on lines 15-17: now that we have RequireJS, we can go ahead and download all the things we&#8217;ve queued already (and call their callbacks if those were passed as arguments).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">RequireJS with WordPress<\/h2>\n\n\n\n<p>There you have it &#8211; RequireJS working nicely with WordPress and all its plugins!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently found myself really wanting to use RequireJS with WordPress, to manage the various JavaScript dependencies a client site had. Unfortunately, this was easier said than done. WordPress is historically not very compatible with RequireJS, as it provides jQuery and a myriad of other JavaScript files out of the box, which are difficult to&#8230; <a class=\"read-more\" href=\"https:\/\/ashton.codes\/use-requirejs-wordpress-plugins-jquery-ui\/\">[Read More]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45,44],"tags":[],"class_list":["post-869","post","type-post","status-publish","format-standard","hentry","category-javascript","category-wordpress"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Use RequireJS with WordPress (plugins) &amp; jQuery (UI) - Chris Ashton - Senior Developer<\/title>\n<meta name=\"description\" content=\"I recently found myself really wanting to use RequireJS with WordPress, to manage the various JavaScript dependencies a client site had. Unfortunately,\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ashton.codes\/use-requirejs-wordpress-plugins-jquery-ui\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Use RequireJS with WordPress (plugins) &amp; jQuery (UI) - Chris Ashton - Senior Developer\" \/>\n<meta property=\"og:description\" content=\"I recently found myself really wanting to use RequireJS with WordPress, to manage the various JavaScript dependencies a client site had. Unfortunately,\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ashton.codes\/use-requirejs-wordpress-plugins-jquery-ui\/\" \/>\n<meta property=\"og:site_name\" content=\"Chris Ashton - Senior Developer\" \/>\n<meta property=\"article:published_time\" content=\"2016-06-19T21:26:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-08-12T17:02:15+00:00\" \/>\n<meta name=\"author\" content=\"ChrisBAshton\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"ChrisBAshton\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ashton.codes\/use-requirejs-wordpress-plugins-jquery-ui\/\",\"url\":\"https:\/\/ashton.codes\/use-requirejs-wordpress-plugins-jquery-ui\/\",\"name\":\"Use RequireJS with WordPress (plugins) & jQuery (UI) - Chris Ashton - Senior Developer\",\"isPartOf\":{\"@id\":\"https:\/\/ashton.codes\/#website\"},\"datePublished\":\"2016-06-19T21:26:04+00:00\",\"dateModified\":\"2019-08-12T17:02:15+00:00\",\"author\":{\"@id\":\"https:\/\/ashton.codes\/#\/schema\/person\/31b9570ec2884de13e1f408a803682ec\"},\"description\":\"I recently found myself really wanting to use RequireJS with WordPress, to manage the various JavaScript dependencies a client site had. Unfortunately,\",\"breadcrumb\":{\"@id\":\"https:\/\/ashton.codes\/use-requirejs-wordpress-plugins-jquery-ui\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/ashton.codes\/use-requirejs-wordpress-plugins-jquery-ui\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/ashton.codes\/use-requirejs-wordpress-plugins-jquery-ui\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/ashton.codes\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Use RequireJS with WordPress (plugins) &#038; jQuery (UI)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/ashton.codes\/#website\",\"url\":\"https:\/\/ashton.codes\/\",\"name\":\"Chris Ashton - Senior Developer\",\"description\":\"Senior Web Developer | Software engineer | A11y evangelist\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/ashton.codes\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/ashton.codes\/#\/schema\/person\/31b9570ec2884de13e1f408a803682ec\",\"name\":\"ChrisBAshton\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/ashton.codes\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/57ddfc6ac09fd027b540a44f6be841d2f5fde83d27d43b25f480c187fd172178?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/57ddfc6ac09fd027b540a44f6be841d2f5fde83d27d43b25f480c187fd172178?s=96&d=mm&r=g\",\"caption\":\"ChrisBAshton\"},\"description\":\"Senior Full Stack Developer working for GDS.\",\"url\":\"https:\/\/ashton.codes\/author\/chrisbashton\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Use RequireJS with WordPress (plugins) & jQuery (UI) - Chris Ashton - Senior Developer","description":"I recently found myself really wanting to use RequireJS with WordPress, to manage the various JavaScript dependencies a client site had. Unfortunately,","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:\/\/ashton.codes\/use-requirejs-wordpress-plugins-jquery-ui\/","og_locale":"en_GB","og_type":"article","og_title":"Use RequireJS with WordPress (plugins) & jQuery (UI) - Chris Ashton - Senior Developer","og_description":"I recently found myself really wanting to use RequireJS with WordPress, to manage the various JavaScript dependencies a client site had. Unfortunately,","og_url":"https:\/\/ashton.codes\/use-requirejs-wordpress-plugins-jquery-ui\/","og_site_name":"Chris Ashton - Senior Developer","article_published_time":"2016-06-19T21:26:04+00:00","article_modified_time":"2019-08-12T17:02:15+00:00","author":"ChrisBAshton","twitter_misc":{"Written by":"ChrisBAshton","Estimated reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/ashton.codes\/use-requirejs-wordpress-plugins-jquery-ui\/","url":"https:\/\/ashton.codes\/use-requirejs-wordpress-plugins-jquery-ui\/","name":"Use RequireJS with WordPress (plugins) & jQuery (UI) - Chris Ashton - Senior Developer","isPartOf":{"@id":"https:\/\/ashton.codes\/#website"},"datePublished":"2016-06-19T21:26:04+00:00","dateModified":"2019-08-12T17:02:15+00:00","author":{"@id":"https:\/\/ashton.codes\/#\/schema\/person\/31b9570ec2884de13e1f408a803682ec"},"description":"I recently found myself really wanting to use RequireJS with WordPress, to manage the various JavaScript dependencies a client site had. Unfortunately,","breadcrumb":{"@id":"https:\/\/ashton.codes\/use-requirejs-wordpress-plugins-jquery-ui\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ashton.codes\/use-requirejs-wordpress-plugins-jquery-ui\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/ashton.codes\/use-requirejs-wordpress-plugins-jquery-ui\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ashton.codes\/"},{"@type":"ListItem","position":2,"name":"Use RequireJS with WordPress (plugins) &#038; jQuery (UI)"}]},{"@type":"WebSite","@id":"https:\/\/ashton.codes\/#website","url":"https:\/\/ashton.codes\/","name":"Chris Ashton - Senior Developer","description":"Senior Web Developer | Software engineer | A11y evangelist","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ashton.codes\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/ashton.codes\/#\/schema\/person\/31b9570ec2884de13e1f408a803682ec","name":"ChrisBAshton","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/ashton.codes\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/57ddfc6ac09fd027b540a44f6be841d2f5fde83d27d43b25f480c187fd172178?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/57ddfc6ac09fd027b540a44f6be841d2f5fde83d27d43b25f480c187fd172178?s=96&d=mm&r=g","caption":"ChrisBAshton"},"description":"Senior Full Stack Developer working for GDS.","url":"https:\/\/ashton.codes\/author\/chrisbashton\/"}]}},"_links":{"self":[{"href":"https:\/\/ashton.codes\/wp-json\/wp\/v2\/posts\/869","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ashton.codes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ashton.codes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ashton.codes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ashton.codes\/wp-json\/wp\/v2\/comments?post=869"}],"version-history":[{"count":12,"href":"https:\/\/ashton.codes\/wp-json\/wp\/v2\/posts\/869\/revisions"}],"predecessor-version":[{"id":1887,"href":"https:\/\/ashton.codes\/wp-json\/wp\/v2\/posts\/869\/revisions\/1887"}],"wp:attachment":[{"href":"https:\/\/ashton.codes\/wp-json\/wp\/v2\/media?parent=869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ashton.codes\/wp-json\/wp\/v2\/categories?post=869"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ashton.codes\/wp-json\/wp\/v2\/tags?post=869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}