Created
February 3, 2019 17:13
-
-
Save pradeep910/e3f8f259903b415aa49fc7e0ff07a860 to your computer and use it in GitHub Desktop.
AMP support for MailChimp for WordPress plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: MailChimp for WordPress - AMP Support | |
* Description: Fix for MailChimp form submission with page update in AMP. | |
* Plugin URI: https://pradeepsonawane.com | |
* Version: 0.1 | |
* Author: Pradeep Sonawane | |
* Author URI: https://pradeepsonawane.com | |
* | |
* @package rtCamp | |
*/ | |
/** | |
* AMP - Mailchimp form error message handling. | |
* | |
* @param object $message Mailchimp Form Object. | |
* | |
* @return void | |
*/ | |
function gk_amp_mailchimp_form_error_handling( $message ) { | |
$post_form_id = intval( wp_unslash( $_POST['_mc4wp_form_id'] ) ); | |
if ( isset( $post_form_id ) && isset( $message->notices[0] ) ) { | |
wp_send_json( [ 'error' => esc_html( $message->notices[0]->text ) ], 300 ); | |
die; | |
} | |
} | |
add_action( 'mc4wp_form_error', 'gk_amp_mailchimp_form_error_handling' ); | |
/** | |
* AMP - Mailchimp form success message handling. | |
* | |
* @param object $message Mailchimp Form Object. | |
* | |
* @return void | |
*/ | |
function gk_amp_mailchimp_form_success_handling( $message ) { | |
$post_form_id = intval( wp_unslash( $_POST['_mc4wp_form_id'] ) ); | |
if ( isset( $post_form_id ) && isset( $message->notices[0] ) ) { | |
wp_send_json( [ 'success' => esc_html( $message->notices[0]->text ) ], 200 ); | |
die; | |
} | |
} | |
add_action( 'mc4wp_form_success', 'gk_amp_mailchimp_form_success_handling' ); | |
/** | |
* Mailchimp Form markup filtering for AMP page. | |
* Add success message in the form markup. | |
* | |
* @param string $output Markup of Mailchimp form. | |
* | |
* @return string | |
*/ | |
function gk_amp_mailchimp_form_filter( $output ) { | |
if ( rt_is_amp() && ! strpos( $output, 'submit-success' ) ) { | |
$output .= '<div submit-success><template type="amp-mustache">{{{success}}}</template></div>'; | |
} | |
return $output; | |
} | |
add_filter( 'mc4wp_form_content', 'gk_amp_mailchimp_form_filter' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment