Create your own button via a shortcode

Here’s a quick little snippet for adding your own shortcode which generates a button in any post or page from a single shortcode. Within the shortcode, there are 2 options to add a url for the button and the text that appears on the button itself.

/**
* Callback to register a button shortcode.
*
* @param array $atts Shortcode attributes.
* @return string Shortcode output.
*/
function pro_button_fnct( $atts ) {
$atts = shortcode_atts(
array(
'url' => '#',
'text' => 'Click me',
), $atts, 'button' );
return '<a href="' . esc_html( $atts['url'] ) . '" class="btn btn-lg btn-primary-outline">' . esc_html( $atts['text'] ).'</a>';
}
add_shortcode( 'button', 'pro_button_fnct' );

 

Then, to add the button anywhere in a post or page, add the following in the editor:

[button url=”/contact” text=”Button text”]

Useful post? Share it

Leave a Reply

Your email address will not be published. Required fields are marked *