Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fix for gravity form submission with page update in AMP native mode (amp-wp plugin)
<?php
/**
* Plugin Name: Gravity form fix for AMP-WP plugin
* Description: Fix for gravity form submission with page update in AMP native mode (amp-wp plugin). Gravity forms submission with page update fix for AMP Native mode of amp-wp plugin. https://ampbyexample.com/components/amp-form/#form-submission-with-page-update
* Plugin URI: https://gist.github.com/pradeep910/b25525034289ab0bc2b7fa0896f4ac90/
* Version: 0.1
* Author: Pradeep Sonawane
* Author URI: https://pradeep910.wordpress.com
*
* @package rtCamp
*/
if ( ! function_exists( 'rt_is_amp_native' ) ) {
/**
* Check if AMP native is enabled.
*
* @return bool
*/
function rt_is_amp_native() {
return ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) ? true : false;
}
}
/**
* Handle gravity form validation errors.
*
* @param array $validation Validation array.
*
* @return array
*/
function rt_gform_handle_validation_error( $validation ) {
if ( empty( $validation['is_valid'] ) && rt_is_amp_native() ) {
if ( ! empty( $validation['form'] ) && ! empty( $validation['form']['fields'] ) ) {
foreach ( $validation['form']['fields'] as $fields ) {
if ( '' !== $fields['validation_message'] ) {
// Add error messages as string.
$errors .= sprintf(
"<p><span class='error-field-name'>%s</span> <span class='error-field-message'>%s</span></p>",
$fields['label'],
str_replace( 'This', '', $fields['validation_message'] )
);
}
}
}
wp_send_json( array(
'error' => $errors
), 300 );
exit();
}
return $validation;
}
add_filter( 'gform_validation', 'rt_gform_handle_validation_error' );
/**
* Support for gravity form submission with page update. https://ampbyexample.com/components/amp-form/#form-submission-with-page-update
*
* @param object $entry The entry that was just created via gravity form.
*
* @param array $form The current form.
*/
function rt_redirect_gform_post_submission( $entry, $form) {
if ( rt_is_amp_native() ) {
$message = empty( $form['confirmation']['message'] ) ? sprintf( 'Thanks, this form has been submitted successfully!', 'rtcamp' ) : $form['confirmation']['message'];
// Send gravity form confirmation message as success message via json.
wp_send_json( array(
'success' => $message
) );
exit();
}
}
add_action( 'gform_after_submission', 'rt_redirect_gform_post_submission', 10, 2 );
/**
* Filter Gravity form markup to add submit-success element.
*
* @param string $form_string Gravity form markup.
* @param array $form Gravity form fields.
* @return string
*/
function rt_amp_gravity_form_filter( $form_string, $form ) {
if( rt_is_amp_native() && ! strpos( $form_string, 'submit-success' ) ) {
$form_string = str_replace(
'</form>',
sprintf(
'<div submit-success>
<template type="amp-mustache">
{{{success}}}
</template>
</div>',
'rtcamp'
),
$form_string
);
}
return $form_string;
}
add_filter( 'gform_get_form_filter', 'rt_amp_gravity_form_filter', 10, 2 );
/**
* Filters gravity form ajax attribute to disable ajax. Props Divyaraj Masani (@divyarajmasani)
*
* @param string $args post content.
* @return array
*/
function rt_no_ajax_on_all_forms( $args ) {
if( rt_is_amp_native() ) {
$args['ajax'] = false;
}
return $args;
}
add_filter('gform_form_args', 'rt_no_ajax_on_all_forms', 10, 1);
// Use client-side validation UI instead of showing validation errors after page reload. Props Rahul Bansal at rtCamp.
add_filter( 'gform_field_content', function( $content, $field ) {
if ( $field->isRequired && rt_is_amp_native() ) {
return str_replace( 'aria-required=', 'required=', $content );
}
return $content;
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.