2020-04-17

We needed a shortcode for buttons so they were easy to stick in post content. Here’s what we did…

Added the following code to /dgtl-custom-admin/inc/shortcodes.php:

// Button Shortcode for Astra Theme
function dgtl_button_shortcode( $atts, $content = null ) {

// shortcode attributes
extract( shortcode_atts( array(
'url' => '',
'title' => '',
'target' => '',
'text' => '',
), $atts ) );

$content = $text ? $text : $content;

// Returns the button with a link
if ( $url ) {

$link_attr = array(
'href' => esc_url( $url ),
'title' => esc_attr( $title ),
'target' => ( 'blank' == $target ) ? '_blank' : '',
'class' => 'custombutton'
);

$link_attrs_str = '';

foreach ( $link_attr as $key => $val ) {

if ( $val ) {

$link_attrs_str .= ' ' . $key . '="' . $val . '"';

}

}


return '<a' . $link_attrs_str . '><button class="ast-button">' . do_shortcode( $content ) . '</button></a>';

}

// Return as span when no link defined
else {

return '<button class="ast-button">' . do_shortcode( $content ) . '</button>';

}

}

add_shortcode( 'button', 'dgtl_button_shortcode' );

Clients can use the shortcode as follows:

Leave a Reply