pierrebalian
9/25/2017 - 5:44 PM

Shortcode for Button with Custom Attributes

Shortcode for Button with Custom Attributes

<?php
/**
 * Adds a [button] shortcode.
 *
 * @param array  $atts  An array of shortcode attributes.
 *
 * @return string
 */
function button_shortcode( $atts, $content = '' ) {

	extract(
		shortcode_atts(
			array(
				'link' => '#',
				'icon' => '',
			), $atts
		)
	);

  /**
   * Adds FontAwesome Icon Markup if the attribute exists, otherwise it outputs nothing
   * 
   * @return string
   */
	if ( ! empty( $icon ) ) {
		$button_icon = '<i class="fa fa-' . $icon . '"></i>';
	} else {
		$button_icon = '';
	}

	return '<a class="button" href="' . $link . '">' . $button_icon . $content . '</a>';
}

function register_shortcodes() {
	add_shortcode( 'button', 'button_shortcode' );
}
add_action( 'init', 'register_shortcodes' );