Skip to content

Early-bird lifetime licence: $199 once, for the first 200 buyers only. See the offer

Button shortcode

Type [button url="/contact/"]Get in touch[/button] to add a button that looks like your theme’s buttons.

  • Type PHP
  • Runs Where you put the [button] shortcode
  • Category Shortcodes
  • Tested with WordPress 7.1
PHP
<?php

// [button url="/contact/" new_tab="no"]Get in touch[/button]
$atts = shortcode_atts( array( 'url' => '#', 'new_tab' => 'no' ), $atts, 'button' );
$tab  = 'yes' === $atts['new_tab'] ? ' target="_blank" rel="noopener"' : '';
$text = '' !== trim( (string) $content ) ? $content : __( 'Read more' );
return '<div class="wp-block-button"><a class="wp-block-button__link wp-element-button" href="' . esc_url( $atts['url'] ) . '"' . $tab . '>' . esc_html( $text ) . '</a></div>';

Good to know

Add new_tab="yes" to open the link in a new tab.

How to use it

Type [button] in a post, a page or a Shortcode block.

In these packs

  • Shortcodes: Handy shortcodes for dates, breadcrumbs, buttons, share links, members-only content and more.

Move your snippets over this afternoon.

Importing changes nothing until you switch over, and your old shortcodes keep working.