# Button Shortcode

> Adds a [button] shortcode that turns a link into a styled button using your theme's button styles, for calls to action inside classic content and widgets.

- Type: PHP
- Runs: Run everywhere
- Category: [Content](https://wpide.com/library/category/content/)
- Tags: shortcode, button, cta
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/button-shortcode/

## Code

```php
add_shortcode( 'button', function ( $atts ) {
    $atts = shortcode_atts( array(
        'url'     => '',
        'text'    => 'Learn more',
        'new_tab' => 'no',
        'class'   => '',
    ), $atts, 'button' );

    if ( '' === $atts['url'] ) {
        return '';
    }

    $target = 'yes' === $atts['new_tab'] ? ' target="_blank" rel="noopener noreferrer"' : '';
    $class  = trim( 'wp-element-button button wpide-button ' . implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $atts['class'] ) ) ) );

    return '<a class="' . esc_attr( $class ) . '" href="' . esc_url( $atts['url'] ) . '"' . $target . '>' . esc_html( $atts['text'] ) . '</a>';
} );
```

## Notes

Example: [button url="/contact/" text="Get in touch" new_tab="no"].

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Button Shortcode" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
