WordPress表单操作与AMP不兼容

3

我想在WordPress中使用AMP和表单操作。 我正在使用POST方法的action-xhr。

当我提交表单时,会出现以下错误:

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

即使尝试添加以下标头也是如此:

header("AMP-Access-Control-Allow-Source-Origin: https://www.demo.com");

问题仍然存在。 有任何建议吗?

我正在使用WordPress版本5.0.3

2个回答

2
您可以使用您的 .htaccess 文件设置标题。
Header set Access-Control-Allow-Origin 'https://cdn.ampproject.org​'
Header set AMP-Access-Control-Allow-Source-Origin 'https://your-domain.com'

这是一个简单的例子给你;
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');

更多信息:在WordPress中创建AMP页面上的评论表单

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接