Search code examples
wordpresswordpress-gutenbergwordpress-plugin-creation

Why does button component in WP editor keep rendering anchor tag in front-end?


I am in the process of developing a block based form plugin for WP. The main form block will support nested blocks - the button core component being one of them.

Problem is that whenever I include the button component, the thing that gets rendered in the front-end is an anchor tag instead. I checked - the link & anchor options are empty in the button sidebar options (in editor).

So shouldn't a button have been rendered instead? What am I missing here?

The official Wordpress reference guide states the following -

The presence of a href prop determines whether an anchor element is rendered instead of a button.

Props not included in this set will be applied to the a or button element

Source: https://developer.wordpress.org/block-editor/reference-guides/components/button/#props)

Also, is there any way to disable the anchor options when I am inserting a button component inside my form?

Wordpress version: 5.8


Solution

  • The core/button block only renders an <a> tag. You can see for yourself in the source code. The core/button block was not intended to create an actual <button> element (for submitting a form like in your example).

    What you'll need to do is create your own custom block that does output a <button> HTML element.