Skip to content

Instantly share code, notes, and snippets.

@surajair
Last active December 27, 2018 09:52
Show Gist options
  • Save surajair/0f8cbfef03c795df4b6094854260ec19 to your computer and use it in GitHub Desktop.
Save surajair/0f8cbfef03c795df4b6094854260ec19 to your computer and use it in GitHub Desktop.
Wordpress AJAX handler for comments form submit on AMP pages
<?php
function amp_comment_submit(){
$comment = wp_handle_comment_submission( wp_unslash( $_POST ) );
if ( is_wp_error( $comment ) ) {
$data = intval( $comment->get_error_data() );
if ( ! empty( $data ) ) {
status_header(500);
wp_send_json(array('msg' => $comment->get_error_message(),
'response' => $data,
'back_link' => true ));
}
}
else {
@header('AMP-Redirect-To: '. get_permalink($_POST['comment_post_ID']));
@header('AMP-Access-Control-Allow-Source-Origin: ' . $_REQUEST['__amp_source_origin'] );
wp_send_json(array('success' => true));
}
}
add_action('wp_ajax_amp_comment_submit', 'amp_comment_submit');
add_action('wp_ajax_nopriv_amp_comment_submit', 'amp_comment_submit');
@rohitkudos
Copy link

Getting this error
Form submission failed: Error: Response must contain the AMP-Access-Control-Allow-Source-Origin header​​​

@rohitkudos
Copy link

See screenshot - http://prntscr.com/m02upk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment