Improve form handling when submission handler does no redirection#2425
Improve form handling when submission handler does no redirection#2425westonruter merged 14 commits intodevelopfrom
Conversation
7a59fd1 to
2fe8860
Compare
I don't think they're helpful for regular users if added to prominently. Maybe at the end in parentheses? e.g.
Looks reasonable and easy to override. Contrast is good too, although it relies on the theme's text color to be dark. Maybe specify color too? |
A concern I have is the That being the case, in the case of a 200 response, what do you think of something like: “It appears your submission was successful.” (Still not very comforting!) But then include a This would be similar for a 400+ status code, except the user message could actually include the status code description (perhaps): “Your submission failed: Bad Request.” This could then include the same Thoughts? |
|
Yeah that could work 👍 |
…on error Explicitly send 200 status code on post_comment_redirect
30cc9d9 to
13dab7f
Compare
| $small->appendChild( $this->dom->createTextNode( $reason ) ); | ||
| $small->appendChild( $this->dom->createTextNode( ' ' ) ); | ||
| $link = $this->dom->createElement( 'a' ); | ||
| $link->setAttribute( 'href', 'https://amp-wp.org/?p=5463' ); |
There was a problem hiding this comment.
Just noting that this page hasn't been published yet.
There was a problem hiding this comment.
Correct. Shortlink to redirect to published page once released.


This follows up on #911/#923.
When a plugin adds a form to the page and the handling of the form submission does not redirect to a success page, the current behavior of the AMP plugin is to just not show any indication at all that a submission was made. The
amp-formclient-side fails because thePOSTresponse contains an HTML page as opposed to the expected JSON. This is bad because many forms in WordPress process submissions without redirections, leaving them to appear as broken. (In contrast, whenwp_redirect()is done, then\AMP_HTTP::intercept_post_request_redirect()will automatically send theAMP-Redirect-Toresponse header, taking the user to the intended success/failure page.)To improve the experience, when processing a form submission (which didn't have
action-xhrto begin with) we need to detect when thePOSTform handler is responding with an HTML body rather than a redirect, and in this case, try to serve some JSON that will populate thesubmit-successandsubmit-errormessage templates, based on whether the form handler returns a 200 or 400+ status code. When a 200 status is returned then a generic success message should be displayed, and when a 400+ status code is returned then a generic error message should be displayed. A challenge here is that form processors may detect an error but they still return with a 200 code, which we can't really do anything about.The ideal is still that a theme/plugin developer adds direct support for forms by adding
action-xhrto theformelements, sending JSON responses, and addingdiv[submitting],div[submit-success], anddiv[submit-error]message templates. But this improves compatibility with themes/plugins that have not yet done this.To test this PR, use this test plugin that has a
[test_form_post]shortcode: https://gist.github.com/westonruter/6c0eb253972ccdfc31f841629e2f4344Before
No message is displayed at all when
200response:Nor in an error response:
After
A new
submittingmessage is displayed:And upon a 200 response, a generic success message is displayed:
And when a 200 response happens but the page is redirecting, a “Redirecting…” message is displayed without the success styling since it could be redirecting to an error page:
If there is a
400+ error response, then thesubmit-errormessage is displayed:Comment Form
There is a new “Submitting” message here as well:
The
submit-erroris now styled in the same way as the generic form conversion:Note the
ERROR:prefix text is coming from WordPress itself.Todo