How to create a custom form in AMP and add it through shortcodes

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 – create a custom plugin by using below the code

<?php
/**
Plugin Name:form For AMP
Plugin URI: https://wordpress.org/plugins/accelerated-mobile-pages/
Description: WPML For AMP - Accelerated Mobile Pages for WPML Support
Version: 1.7.2
Author: Ahmed Kaludi, Mohammed Kaludi
Author URI: https://ampforwp.com/
Donate link: https://www.paypal.me/Kaludi/25usd
License: GPL2+
Text Domain: accelerated-mobile-pages-WPML-support
 */
function renderField( $opts ) {
$length_insert = '';
if (isset($opts['max-length'])) {
$length_insert .= ' maxlength="' . $opts['max-length'] . '"';
}
if (isset($opts['min-length'])) {
$length_insert .= ' minlength="' . $opts['min-length'] . '"';
}
return '<input type="' . $opts['type'] . '" name="' . $opts['name'] . '" placeholder="' . $opts['label'] . '" title="' . $opts['label'] . '" class="txbx"' . $length_insert . ' />';
}
function ampforwp_form( $atts ) {
$options = shortcode_atts( array(
'title' => 'ampforwp form',
'description' => 'This is short description about Form',
), $atts);
$fields = [
['name' => 'name', 'label' => 'Name', 'type' => 'text', 'max-length' => 255],
['name' => 'email', 'label' => 'Email address', 'type' => 'email'],
['name' => 'phone', 'label' => 'Phone number', 'type' => 'tel', 'max-length' => 15],
['name' => 'zip', 'label' => 'Zip code', 'type' => 'number', 'max-length' => 10],
['name' => 'acres', 'label' => 'Total acreage of your land', 'type' => 'number'],
];
$fields_html = implode("", array_map('renderField', $fields));
$fields_html .= '<button type="submit" class="btn">Learn more</button>';
$messages = '<div submit-success>
<template type="amp-mustache">
Success!
</template>
</div>
<div submit-error>
<template type="amp-mustache">
Form failed to submit!
</template>
</div>';
return '<hr/><h1>' . $options['title'] . '</h1><p>' . $options['description'] . '</p><form class="ampforwp-form-allow" target="_top" method="POST" action-xhr="/">' . $fields_html . $messages . '</form>';
}
add_shortcode('ampforwp-form-shortcode', 'ampforwp_form');
 

Step 2 – Open a post in the block editor or add a new one and then click on the + button and pick the shortcode block

Step 3 – After that Add shortcode like below the screenshot

Step 4 – Once you are done, click on the “publish” button.

The output of custom form in AMP and add it through shortcodes

That’s it for this tutorial!

We hope it helped you. If you have any feedback or questions, then please share them in the comments section below.

Share
Published by
Sonali R
Article Categories: How To

How to create an AWeber form in AMP

If you want to add an AWeber form in AMP then first you need to…

3 years ago

How to setup webpushr notification in AMP

Webpushr is the fastest growing web push notifications platform that marketers love & developers rely…

3 years ago

How to add Adobe Analytics in AMP

Follow these steps to add Adobe Analytics in AMP Step 1: Go to WordPress Admin Area -> AMP…

4 years ago

What is the CWV plugin all about

Core Web Vitals are the subset of Web Vitals that apply to all web pages,…

4 years ago

How to fix “Alternate page with proper canonical tag” Page Index issue in Google Search Console

The following reasons can be shown for non-indexing. Server error (5xx) Your server returned a 500-level…

4 years ago

Google Analytics ( GA4 ) integration in AMPforWP

We have added the Google Analytics 4 ( GA 4 ) integration in AMPforWP v1.0.80.…

4 years ago