Mastering the shortcode api

2,870 views

Published on

This presentation walks you through how to create and use custom shortcodes throughout your WordPress website, adding in anything from a simple header to an advanced nested layout.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,870
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
14
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Mastering the shortcode api

  1. 1. Mastering the Shortcode API Peter Baylies Semper Fi Web DesignMonday, November 7, 11
  2. 2. What’s a shortcode • It’s a tag that can dynamically insert content into your wordpress site. It should look sort of like this: [gallery]Monday, November 7, 11
  3. 3. How do I make them? • with the add_shortcode() function • arguments: your shortcode’s name and your PHP function that it calls: add_shortcode(‘tag’,‘my_function_name’);Monday, November 7, 11
  4. 4. Wait, my PHP function? • Yes, it can be passed up to three arguments: my_function_name($atts,$content,$code); • arguments passed to your shortcode • content contained within your shortcode • the name of the shortcode itselfMonday, November 7, 11
  5. 5. Attributes • Shortcode attributes look like this: [gallery id=”123” columns=”4”] • They get processed with shortcode_atts() extract ( shortcode_atts ( array ( ‘id’ => null, ‘columns’=>3 ), $atts ) ); • This sets defaults and validates arguments.Monday, November 7, 11
  6. 6. Content • Shortcodes can enclose content too. [caption]This is a caption.[/caption] The text in between the shortcode tags gets passed to $content for processing.Monday, November 7, 11
  7. 7. Code • The name of the shortcode called. • This gives you the ability to write one function to handle multiple shortcodes. • This helps avoid duplication of code.Monday, November 7, 11
  8. 8. Removing a shortcode: • remove_shortcode() • Lets you unregister an existing shortcode • This lets you replace other shortcodes with your own custom code.Monday, November 7, 11
  9. 9. Running a shortcode: •do_shortcode() • This handles shortcode processing. • Run it on $content to nest shortcodes: [loop] [title] [content] [/loop]Monday, November 7, 11
  10. 10. That’s It! • Now go forth and write shortcodes! • Any Questions?Monday, November 7, 11

×