patric-boehner
8/29/2016 - 7:37 AM

Minimal example of a Short Code

Minimal example of a Short Code

<?php
//* Do NOT include the opening php tag shown above. Copy the code shown below.


// Minimal Shortcode
add_shortcode( 'cta_button', 'pb_register_button_shortcode' );
function pb_register_button_shortcode( $atts ){
  
  /*
   * We return a pre formatted string with no variables.
   * Works but obviously not very useful
   */

	return '<a class="button" href="http://example.com">Button</a>';

}

//*************************************

// Simple Shortcode
add_shortcode( 'cta_button', 'pb_register_button_shortcode' );
function pb_register_button_shortcode( $atts ){
  
  /*
   * Now we add variable to make the shortcode more useful.
   * One for text and one for a url.
   */

	extract( shortcode_atts(
		array(
			'text' => 'Title',
			'url'	  => ''
		),
		$atts
	));

	//Output the shortcode
	return '<a class="button" href="' .$url. '">' .$text. '</a>';

}