Last active
December 27, 2018 09:52
-
-
Save surajair/0f8cbfef03c795df4b6094854260ec19 to your computer and use it in GitHub Desktop.
Wordpress AJAX handler for comments form submit on AMP pages
This file contains hidden or 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 | |
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'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Getting this error
Form submission failed: Error: Response must contain the AMP-Access-Control-Allow-Source-Origin header