{"id":15555,"date":"2019-12-31T09:57:43","date_gmt":"2019-12-31T09:57:43","guid":{"rendered":"https:\/\/ampforwp.com\/tutorials\/?post_type=ht_kb&#038;p=15555"},"modified":"2020-05-08T06:27:17","modified_gmt":"2020-05-08T06:27:17","slug":"how-to-add-custom-scripts-in-amp","status":"publish","type":"ht_kb","link":"https:\/\/ampforwp.com\/tutorials\/article\/how-to-add-custom-scripts-in-amp\/","title":{"rendered":"How to add custom scripts in AMP"},"content":{"rendered":"\n<p>There are two ways to include the custom scripts in AMP and in this tutorial I will show you both ways to include the script in AMP.<\/p>\n\n\n\n<ol><li><strong><a href=\"https:\/\/ampforwp.com\/tutorials\/article\/how-to-add-custom-scripts-in-amp\/#1-external-scripts\">External  Scripts <\/a><\/strong><\/li><li><strong><a href=\"https:\/\/ampforwp.com\/tutorials\/article\/how-to-add-custom-scripts-in-amp\/#2-inline-scripts\">Inline Scripts<\/a><\/strong><\/li><\/ol>\n\n\n\n<p>You can add the script with the help of amp-script tag and this lets you to add custom JavaScript to your AMP page while still retaining its lightning-fast speed!<\/p>\n\n\n\n<h4><mark style=\"background-color:#fff9c4;\"><strong>1 &#8211; External  Scripts <\/strong><\/mark><\/h4>\n\n\n\n<p>An amp-script element can load a JavaScript file from a URL. This is usually the best way to integrate a script as the browser needs to download the script only once.<\/p>\n\n\n\n<p>Below the script, you need to add posts or pages you want to display amp-script<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;htmlmixed&quot;,&quot;mime&quot;:&quot;text\/html&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;HTML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;html&quot;}\">&lt;amp-script layout=&quot;container&quot; src=&quot;https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/amp-scripts\/custom_amp.js&quot; class=&quot;amp-script-sample&quot;&gt;\n&lt;button id=&quot;hello&quot;&gt;Click!&lt;\/button&gt;\n&lt;\/amp-script&gt;<\/pre><\/div>\n\n\n\n<p> <br>There is the script in custom_amp.js and in that file, you need to add the scripts like <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;JavaScript&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;js&quot;}\">const button = document.querySelector('#hello');\nbutton.addEventListener('click', () =&gt; {\n  const h1 = document.createElement('h1');\n  h1.textContent = 'Hello World!';\n  document.body.appendChild(h1);\n});\n<\/pre><\/div>\n\n\n\n<h4><mark style=\"background-color:#fff9c4;\"><strong> 2 &#8211; Inline Scripts<\/strong><\/mark><\/h4>\n\n\n\n<p>You can also declare scripts inline and reference them by id  and<br>Note that the type needs to be&nbsp;<code>type=text\/plain<\/code>&nbsp;and the&nbsp;<code>target=amp-script<\/code>  like &#8211;  <br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;JavaScript&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;js&quot;}\">&lt;amp-script layout=&quot;container&quot; script=&quot;inline_amp&quot; class=&quot;amp-script-sample&quot;&gt;\n  &lt;button id=&quot;hello2&quot;&gt;Click!&lt;\/button&gt;\n&lt;\/amp-script&gt;\n&lt;script id=&quot;inline_amp&quot; type=&quot;text\/plain&quot; target=&quot;amp-script&quot;&gt;\n  const button = document.querySelector('#hello2');\n  button.addEventListener('click', () =&gt; {\n    const h1 = document.createElement('h1');\n    h1.textContent = 'Hello World 2!';\n    document.body.appendChild(h1);\n  });\n&lt;\/script&gt;<\/pre><\/div>\n\n\n\n<p>Once you created the script then the script won&#8217;t work and you will have some error in the console and you just need to add the amp-script-src meta tag  <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"367\" src=\"https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/02\/4-5-1024x367.png\" alt=\"\" class=\"wp-image-16518\" srcset=\"https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/02\/4-5-1024x367.png 1024w, https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/02\/4-5-300x108.png 300w, https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/02\/4-5-768x275.png 768w, https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/02\/4-5-50x18.png 50w, https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/02\/4-5-60x22.png 60w, https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/02\/4-5-100x36.png 100w, https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/02\/4-5.png 1364w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Once you see this error then you just need to copy the key like &#8220;<strong>sha384 JJ8hybcP8hhYy9N0ruZaJF5DqeLX5R5Aa1Kd4OPrB26C_2Gy33RFc4Z_IBQel0MA<\/strong>&#8221;   and then you need to go to  <strong>WordPress Dashboard<\/strong> -&gt;<strong> Settings<\/strong> -&gt;  <strong>Advance Settings<\/strong>  -&gt;  <strong>Enter HTML in Head<\/strong> and then add the meta tag like <br>  <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;htmlmixed&quot;,&quot;mime&quot;:&quot;text\/html&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;HTML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;html&quot;}\">&lt;meta\n    name=&quot;amp-script-src&quot;\n    content=&quot;sha384-YCFs8k-ouELcBTgzKzNAujZFxygwiqimSqKK7JqeKaGNflwDxaC3g2toj7s_kxWG&quot;\n  \/&gt;<\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"508\" src=\"https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/02\/5-7-1024x508.png\" alt=\"\" class=\"wp-image-16546\" srcset=\"https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/02\/5-7-1024x508.png 1024w, https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/02\/5-7-300x149.png 300w, https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/02\/5-7-768x381.png 768w, https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/02\/5-7-50x25.png 50w, https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/02\/5-7-60x30.png 60w, https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/02\/5-7-100x50.png 100w, https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/02\/5-7.png 1303w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Once you add that meta tag then your amp-script will start working in AMP<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2019\/12\/script.mp4\"><\/video><\/figure>\n\n\n\n<p><strong>That\u2019s it for this tutorial!<\/strong><\/p>\n\n\n\n<p>We hope it helped you. If you have any feedback or questions, then please share them in the comments section below.<\/p>\n<div id=\"div-1\" class=\"hkb-feedback\">\n\t\t\t<strong class=\"ht-mobile-exit__title\">Ask your Questions Directly!<\/strong><br>\n\t\t\t<div class=\"hkb_widget_exit__content\" style=\"font-size: 14px;\">If you can't find the solutions in this article, then just ask us in the comments and we will answer it for you!<\/div>\n\t\t\t<a href=\"https:\/\/ampforwp.com\/support\/#contact\" id=\"button\" target=\"_blank\" rel=\"noopener noreferrer\"><button>Yes, I have a Question<\/button><\/a>\n\t\t<\/div>","protected":false},"excerpt":{"rendered":"<p>There are two ways to include the custom scripts in AMP and in this tutorial I will show you both ways to include the script in AMP. External Scripts Inline Scripts You can add the script with the help of amp-script tag and this lets you to add custom JavaScript&#8230;<\/p>\n","protected":false},"author":13,"comment_status":"open","ping_status":"closed","template":"","format":"standard","meta":[],"ht-kb-category":[34],"ht-kb-tag":[],"_links":{"self":[{"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/ht-kb\/15555"}],"collection":[{"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/ht-kb"}],"about":[{"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/types\/ht_kb"}],"author":[{"embeddable":true,"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/comments?post=15555"}],"version-history":[{"count":70,"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/ht-kb\/15555\/revisions"}],"predecessor-version":[{"id":17291,"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/ht-kb\/15555\/revisions\/17291"}],"wp:attachment":[{"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/media?parent=15555"}],"wp:term":[{"taxonomy":"ht_kb_category","embeddable":true,"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/ht-kb-category?post=15555"},{"taxonomy":"ht_kb_tag","embeddable":true,"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/ht-kb-tag?post=15555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}