Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AMP shim support for Gravity Form submissions
<?php
/*
* Plugin Name: AMP shim support for Gravity Form submissions
* Plugin URI: https://gist.github.com/westonruter/b2554073461707a88057bb86b6b603d6
* Version: 0.1.0
* Author: Weston Ruter
* Author URI: https://weston.ruter.net/
* License: GNU General Public License v2 (or later)
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
// Shim support for Gravity Form submissions on AMP pages.
add_action( 'gform_post_submission', function() {
$location = null;
foreach ( headers_list() as $header ) {
if ( preg_match( '/^Location:\s*(.+)/i', $header, $matches ) ) {
$location = $matches[1];
break;
}
}
if ( $location ) {
header_remove( 'Location' );
header( "AMP-Redirect-To: $location" );
header( 'AMP-Access-Control-Allow-Source-Origin: ' . home_url() );
header( 'Access-Control-Expose-Headers: AMP-Redirect-To, AMP-Access-Control-Allow-Source-Origin' );
exit;
}
} );
// 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 ) {
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.