{"id":17017,"date":"2020-04-02T10:15:21","date_gmt":"2020-04-02T10:15:21","guid":{"rendered":"https:\/\/ampforwp.com\/tutorials\/?post_type=ht_kb&#038;p=17017"},"modified":"2020-04-29T12:09:58","modified_gmt":"2020-04-29T12:09:58","slug":"how-to-create-a-custom-form-in-amp-and-add-it-through-shortcodes","status":"publish","type":"ht_kb","link":"https:\/\/ampforwp.com\/tutorials\/article\/how-to-create-a-custom-form-in-amp-and-add-it-through-shortcodes\/","title":{"rendered":"How to create a custom form in AMP and add it through shortcodes"},"content":{"rendered":"\n<p>If you want to create custom form in AMP and add it through shortcodes in the content for this follow the below steps<\/p>\n\n\n\n<p><strong>Step 1 &#8211; <\/strong> create a custom plugin by using below the code<\/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;}\">&lt;?php\n\/**\nPlugin Name:form For AMP\nPlugin URI: https:\/\/wordpress.org\/plugins\/accelerated-mobile-pages\/\nDescription: WPML For AMP - Accelerated Mobile Pages for WPML Support\nVersion: 1.7.2\nAuthor: Ahmed Kaludi, Mohammed Kaludi\nAuthor URI: https:\/\/ampforwp.com\/\nDonate link: https:\/\/www.paypal.me\/Kaludi\/25usd\nLicense: GPL2+\nText Domain: accelerated-mobile-pages-WPML-support\n *\/\nfunction renderField( $opts ) {\n$length_insert = '';\nif (isset($opts['max-length'])) {\n$length_insert .= ' maxlength=&quot;' . $opts['max-length'] . '&quot;';\n}\nif (isset($opts['min-length'])) {\n$length_insert .= ' minlength=&quot;' . $opts['min-length'] . '&quot;';\n}\nreturn '&lt;input type=&quot;' . $opts['type'] . '&quot; name=&quot;' . $opts['name'] . '&quot; placeholder=&quot;' . $opts['label'] . '&quot; title=&quot;' . $opts['label'] . '&quot; class=&quot;txbx&quot;' . $length_insert . ' \/&gt;';\n}\nfunction ampforwp_form( $atts ) {\n$options = shortcode_atts( array(\n'title' =&gt; 'ampforwp form',\n'description' =&gt; 'This is short description about Form',\n), $atts);\n$fields = [\n['name' =&gt; 'name', 'label' =&gt; 'Name', 'type' =&gt; 'text', 'max-length' =&gt; 255],\n['name' =&gt; 'email', 'label' =&gt; 'Email address', 'type' =&gt; 'email'],\n['name' =&gt; 'phone', 'label' =&gt; 'Phone number', 'type' =&gt; 'tel', 'max-length' =&gt; 15],\n['name' =&gt; 'zip', 'label' =&gt; 'Zip code', 'type' =&gt; 'number', 'max-length' =&gt; 10],\n['name' =&gt; 'acres', 'label' =&gt; 'Total acreage of your land', 'type' =&gt; 'number'],\n];\n$fields_html = implode(&quot;&quot;, array_map('renderField', $fields));\n$fields_html .= '&lt;button type=&quot;submit&quot; class=&quot;btn&quot;&gt;Learn more&lt;\/button&gt;';\n$messages = '&lt;div submit-success&gt;\n&lt;template type=&quot;amp-mustache&quot;&gt;\nSuccess!\n&lt;\/template&gt;\n&lt;\/div&gt;\n&lt;div submit-error&gt;\n&lt;template type=&quot;amp-mustache&quot;&gt;\nForm failed to submit!\n&lt;\/template&gt;\n&lt;\/div&gt;';\nreturn '&lt;hr\/&gt;&lt;h1&gt;' . $options['title'] . '&lt;\/h1&gt;&lt;p&gt;' . $options['description'] . '&lt;\/p&gt;&lt;form class=&quot;ampforwp-form-allow&quot; target=&quot;_top&quot; method=&quot;POST&quot; action-xhr=&quot;\/&quot;&gt;' . $fields_html . $messages . '&lt;\/form&gt;';\n}\nadd_shortcode('ampforwp-form-shortcode', 'ampforwp_form');\n <\/pre><\/div>\n\n\n\n<p><strong>Step 2 &#8211;<\/strong> Open a post in the block editor or add a new one and then click on the + button and pick the shortcode block <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"929\" height=\"514\" src=\"https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/04\/11.png\" alt=\"\" class=\"wp-image-17019\" srcset=\"https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/04\/11.png 929w, https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/04\/11-300x166.png 300w, https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/04\/11-768x425.png 768w, https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/04\/11-50x28.png 50w, https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/04\/11-60x33.png 60w, https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/04\/11-100x55.png 100w\" sizes=\"(max-width: 929px) 100vw, 929px\" \/><\/figure>\n\n\n\n<p><strong>Step 3 &#8211;<\/strong> After that Add shortcode like below the screenshot<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"462\" src=\"https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/04\/13-1024x462.png\" alt=\"\" class=\"wp-image-17023\" srcset=\"https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/04\/13-1024x462.png 1024w, https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/04\/13-300x135.png 300w, https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/04\/13-768x346.png 768w, https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/04\/13-50x23.png 50w, https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/04\/13-60x27.png 60w, https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/04\/13-100x45.png 100w, https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/04\/13.png 1349w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p> <strong>Step 4 &#8211;<\/strong> Once you are done, click on the \u201c<strong>publish<\/strong>\u201d button. <\/p>\n\n\n\n<h4>The output of custom form in AMP and add it through shortcodes<\/h4>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"462\" src=\"https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/04\/14-1024x462.png\" alt=\"\" class=\"wp-image-17024\" srcset=\"https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/04\/14-1024x462.png 1024w, https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/04\/14-300x135.png 300w, https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/04\/14-768x346.png 768w, https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/04\/14-50x23.png 50w, https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/04\/14-60x27.png 60w, https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/04\/14-100x45.png 100w, https:\/\/ampforwp.com\/tutorials\/wp-content\/uploads\/2020\/04\/14.png 1349w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/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>If you want to create custom form in AMP and add it through shortcodes in the content for this follow the below steps Step 1 &#8211; create a custom plugin by using below the code Step 2 &#8211; Open a post in the block editor or add a new one&#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\/17017"}],"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=17017"}],"version-history":[{"count":12,"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/ht-kb\/17017\/revisions"}],"predecessor-version":[{"id":17217,"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/ht-kb\/17017\/revisions\/17217"}],"wp:attachment":[{"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/media?parent=17017"}],"wp:term":[{"taxonomy":"ht_kb_category","embeddable":true,"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/ht-kb-category?post=17017"},{"taxonomy":"ht_kb_tag","embeddable":true,"href":"https:\/\/ampforwp.com\/tutorials\/wp-json\/wp\/v2\/ht-kb-tag?post=17017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}