* Server-side rendering of the `core/button` block.
* Renders the `core/button` block on the server,
* @param array $attributes The block attributes.
* @param string $content The block content.
* @return string The block content.
function render_block_core_button( $attributes, $content ) {
$p = new WP_HTML_Tag_Processor( $content );
* The button block can render an `<a>` or `<button>` and also has a
* `<div>` wrapper. Find the a or button tag.
while ( $p->next_tag() ) {
if ( 'A' === $tag || 'BUTTON' === $tag ) {
* If this happens, the likelihood is there's no block content,
* or the block has been modified by a plugin.
// If the next token is the closing tag, the button is empty.
while ( $p->next_token() && $tag !== $p->get_token_name() && $is_empty ) {
if ( '#comment' !== $p->get_token_type() ) {
* Anything else implies this is not empty.
* This might include any text content (including a space),
* inline images or other HTML.
* When there's no text, render nothing for the block.
* See https://github.com/WordPress/gutenberg/issues/17221 for the
* Registers the `core/button` block on server.
function register_block_core_button() {
register_block_type_from_metadata(
'render_callback' => 'render_block_core_button',
add_action( 'init', 'register_block_core_button' );