Skip to content

Instantly share code, notes, and snippets.

@patric-boehner
Last active August 30, 2016 19:27
Show Gist options
  • Save patric-boehner/943bd5ec49ae59d28e20e089a20b3903 to your computer and use it in GitHub Desktop.
Save patric-boehner/943bd5ec49ae59d28e20e089a20b3903 to your computer and use it in GitHub Desktop.
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>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment