Mastering the shortcode api

10
Mastering the Shortcode API Peter Baylies Semper Fi Web Design Monday, November 7, 11

description

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.

Transcript of Mastering the shortcode api

Page 1: Mastering the shortcode api

Mastering the Shortcode API

Peter Baylies

Semper Fi Web Design

Monday, November 7, 11

Page 2: Mastering the shortcode api

What’s a shortcode

• It’s a tag that can dynamically insertcontent into your wordpress site.

It should look sort of like this:

[gallery]

Monday, November 7, 11

Page 3: Mastering the shortcode api

How do I make them?

• with the add_shortcode() function

• arguments: your shortcode’s nameand your PHP function that it calls:

add_shortcode(‘tag’,‘my_function_name’);

Monday, November 7, 11

Page 4: Mastering the shortcode api

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 itself

Monday, November 7, 11

Page 5: Mastering the shortcode api

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

Page 6: Mastering the shortcode api

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

Page 7: Mastering the shortcode api

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

Page 8: Mastering the shortcode api

Removing a shortcode:

• remove_shortcode()

• Lets you unregister an existing shortcode

• This lets you replace other shortcodeswith your own custom code.

Monday, November 7, 11

Page 9: Mastering the shortcode api

Running a shortcode:

•do_shortcode()

• This handles shortcode processing.

• Run it on $content to nest shortcodes:

[loop] [title] [content][/loop]

Monday, November 7, 11

Page 10: Mastering the shortcode api

That’s It!

• Now go forth and write shortcodes!

• Any Questions?

Monday, November 7, 11