Last active
August 30, 2016 19:27
-
-
Save patric-boehner/943bd5ec49ae59d28e20e089a20b3903 to your computer and use it in GitHub Desktop.
Minimal example of a Short Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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