{"id":15145,"date":"2019-12-02T09:26:31","date_gmt":"2019-12-02T09:26:31","guid":{"rendered":"https:\/\/ampforwp.com\/tutorials\/?post_type=ht_kb&#038;p=15145"},"modified":"2020-05-08T06:20:30","modified_gmt":"2020-05-08T06:20:30","slug":"how-to-write-a-custom-shortcode-for-amp","status":"publish","type":"ht_kb","link":"https:\/\/ampforwp.com\/tutorials\/article\/how-to-write-a-custom-shortcode-for-amp\/","title":{"rendered":"How to create a custom shortcode for AMP?"},"content":{"rendered":"\n<p>In this tutorial, we will show you how to create a custom shortcode in AMP and to add that follows the below instructions.<\/p>\n\n\n\n<p>By using the below code you can create your own new shortcode<\/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;php&quot;,&quot;mime&quot;:&quot;text\/x-php&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;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">add_shortcode( string $tag, callable $callback )  \n<\/pre><\/div>\n\n\n\n<h4 id=\"parameters\">Parameters<\/h4>\n\n\n\n<p><strong>$tag<\/strong><br> (string) (Required) Shortcode tag to be searched in post content.<\/p>\n\n\n\n<p><strong>$callback<\/strong><br> (callable) (Required) The callback function to run when the shortcode is found. Every shortcode callback is passed three parameters by default, including an array of attributes ($atts), the shortcode content or null if not set ($content), and finally the shortcode tag itself ($shortcode_tag), in that order.<\/p>\n\n\n\n<p><a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/add_shortcode\/\">Click Here<\/a> for more info about add_shortcode function<\/p>\n\n\n\n<p>But if your shortcode consists of form tag or other some HTML tags which are not proper as per AMP then our blacklist sanitizer will strip it and if you really need those tags then you need to override it and to do that you can follow this tutorial \u2013<br><a rel=\"noreferrer noopener\" href=\"https:\/\/ampforwp.com\/tutorials\/article\/override-blacklist-sanitizer\/\" target=\"_blank\">https:\/\/ampforwp.com\/tutorials\/article\/override-blacklist-sanitizer\/<\/a><br> <\/p>\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\n\n\n<p><\/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>In this tutorial, we will show you how to create a custom shortcode in AMP and to add that follows the below instructions. By using the below code you can create your own new shortcode Parameters $tag (string) (Required) Shortcode tag to be searched in post content. $callback (callable) (Required)&#8230;<\/p>\n","protected":false},"author":18,"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\/15145"}],"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\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/comments?post=15145"}],"version-history":[{"count":14,"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/ht-kb\/15145\/revisions"}],"predecessor-version":[{"id":17290,"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/ht-kb\/15145\/revisions\/17290"}],"wp:attachment":[{"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/media?parent=15145"}],"wp:term":[{"taxonomy":"ht_kb_category","embeddable":true,"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/ht-kb-category?post=15145"},{"taxonomy":"ht_kb_tag","embeddable":true,"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/ht-kb-tag?post=15145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}