Skip to content

Forms: default the Button block to a submit element inside a Form - #51046

Open
enejb wants to merge 1 commit into
trunkfrom
fix/forms-button-default-element
Open

Forms: default the Button block to a submit element inside a Form#51046
enejb wants to merge 1 commit into
trunkfrom
fix/forms-button-default-element

Conversation

@enejb

@enejb enejb commented Aug 4, 2026

Copy link
Copy Markdown
Member

Fixes FORMS-741

Proposed changes

A Jetpack Button block that has no element attribute falls back to rendering an <a> (see get_attribute() in projects/plugins/jetpack/extensions/blocks/button/button.php). Inside a Form that produces a submit control that looks right but is actually an anchor:

<a class="wp-block-button__link" href="#" target="_blank" role="button" rel="noopener noreferrer">Submit</a>

Clicking it opens a blank tab and the form is never submitted. The Form block's inner-block template does set element: "button", so this only bites forms whose saved markup predates that or lost the attribute along the way.

  • Button block: make that fallback filterable via a new jetpack_button_default_element filter, instead of hardcoding 'a'. Nothing changes on its own — the default is still a, and the render callback's existing allowlist still ignores anything outside a/button/input.
  • Forms: hook that filter to return button for the duration of a form's render — added in pre_render_contact_form() (before the inner blocks render) and removed in gutenblock_render_form() (once they have). A Button that sets element explicitly is untouched either way, since the filter only supplies the fallback.

Scoping to the form's render span means it covers a button nested in a Group or any other wrapper inside the form, and stops applying the moment the form is done — a Calendly, Eventbrite, Payment Button or Subscribe block elsewhere on the page still renders its anchor.

gutenblock_render_form() has several early returns, so its body moved to a private render_form() and the public method became a thin wrapper that removes the filter. The public signature is unchanged.

Two deliberate non-changes:

  • This is render-time only. The editor is visually unchanged (the block's edit.jsx draws a RichText regardless of element) and saved post content is not rewritten — PHP simply supplies the fallback on every render, so no posts are dirtied on load.
  • saveInPostContent mode still always renders an <a>. That is intentional and documented in the Button block's README (stricter WordPress.com sanitization rules), and the render callback returns early before the fallback is consulted.

One known limitation worth a reviewer's eye: if a third-party pre_render_block filter at a priority above 10 short-circuits the form block, the render callback never runs and the filter would stay attached for the rest of the request. Our own synced-form (ref) path calls gutenblock_render_form() directly, so it still cleans up after itself.

Related product discussion/links

Does this pull request change what data or activity we track or use?

No.

Testing instructions

You need a Form whose Button block has no element attribute — this is the legacy shape, and the editor will not produce it for you.

  • Create a post, switch to the code editor (⌥⌘M / Ctrl+Shift+Alt+M), and paste this — it puts a plain Button before and after the form so you can check the scoping at the same time:
<!-- wp:jetpack/button {"text":"Before form button"} /-->
<!-- wp:jetpack/contact-form -->
<!-- wp:jetpack/field-name {"required":true,"label":"Name"} /-->
<!-- wp:jetpack/button {"text":"Submit"} /-->
<!-- /wp:jetpack/contact-form -->
<!-- wp:jetpack/button {"text":"After form button"} /-->
  • Publish and view the post on the front end, then view source.
  • Before this change: all three render as <a class="wp-block-button__link" href="#" target="_blank" role="button">. Fill in the Name field and click Submit — a blank tab opens, nothing is submitted, and no new entry appears under Feedback / Form Responses.
  • After this change:
    • "Before form button" → still <a ... href="#">
    • "Submit" → <button class="wp-block-button__link" type="submit">
    • "After form button" → still <a ... href="#">
  • Fill in Name and click Submit — the form submits and a new response is recorded.
  • Check nothing else regressed:
    • Wrap the submit button in a Group block inside the Form — it should still render as <button type="submit">.
    • A Button with an explicit element inside the Form, e.g. <!-- wp:jetpack/button {"element":"a","text":"Link"} /-->, must still render as an <a>.
    • A Calendly, Eventbrite, Payment Button or Subscribe block should still render its anchor as before.
  • Run the unit tests: jp test php packages/forms

Verified live on a Jurassic Ninja site: the three buttons render as anchor / button / anchor exactly as above, and a real form submission created a response (0 → 1).

@enejb enejb added Bug When a feature is broken and / or not performing as intended [Feature] Forms [Status] Needs Review This PR is ready for review. labels Aug 4, 2026
@enejb enejb self-assigned this Aug 4, 2026
@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack), and enable the fix/forms-button-default-element branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack fix/forms-button-default-element

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!


Jetpack plugin:

No scheduled milestone found for this plugin.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.

@jp-launch-control

jp-launch-control Bot commented Aug 4, 2026

Copy link
Copy Markdown

Code Coverage Summary

Coverage changed in 2 files.

File Coverage Δ% Δ Uncovered
projects/plugins/jetpack/extensions/blocks/button/button.php 18/206 (8.74%) -0.09% 2 ❤️‍🩹
projects/packages/forms/src/blocks/contact-form/class-contact-form-block.php 624/690 (90.43%) 0.07% 0 💚

Full summary · PHP report · JS report

@enejb
enejb force-pushed the fix/forms-button-default-element branch from 315dae0 to ee29d98 Compare August 4, 2026 23:20
@github-actions github-actions Bot added [Block] Button [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ labels Aug 4, 2026
@enejb
enejb requested a review from Copilot August 4, 2026 23:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes legacy Jetpack Forms posts where an inner Jetpack Button block is missing the element attribute, causing it to render as an anchor (<a>) and therefore never submit the form. It introduces a filterable fallback element for the Button block and scopes a Forms override to only the duration of a form render.

Changes:

  • Add a new jetpack_button_default_element filter used when the Jetpack Button block has no explicit element attribute.
  • In Jetpack Forms, temporarily hook that filter during form rendering so element-less Buttons inside a form default to a submit-capable <button>, then remove the filter afterward.
  • Add PHPUnit coverage to verify the filter is applied during form render and removed afterward.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
projects/plugins/jetpack/extensions/blocks/button/button.php Adds a filter for the default element fallback used by the Button block when the attribute is absent.
projects/plugins/jetpack/changelog/fix-forms-button-default-element Adds Jetpack plugin changelog entry for the new filter.
projects/packages/forms/src/blocks/contact-form/class-contact-form-block.php Adds/removes the Button default-element filter around form rendering; refactors render to ensure cleanup occurs.
projects/packages/forms/tests/php/contact-form/Contact_Form_Block_Test.php Adds unit tests to confirm filter scoping behavior.
projects/packages/forms/changelog/fix-forms-button-default-element Adds Forms package changelog entry describing the user-facing fix.

Comment on lines +817 to +821
// A Button block that doesn't specify an element renders as an `a`, which can
// never submit the form. Default it to `button` for as long as this form is
// rendering; gutenblock_render_form() drops the filter again once it is done.
add_filter( 'jetpack_button_default_element', array( __CLASS__, 'submit_button_element' ) );

Significance: patch
Type: other

Button block: add a filter for the default HTML element the block falls back to when it does not specify one.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Block] Button [Block] Contact Form Bug When a feature is broken and / or not performing as intended [Feature] Contact Form [Feature] Forms [Package] Forms [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Status] Needs Review This PR is ready for review. [Tests] Includes Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants