Shortcodes allow dynamic content insertion into WordPress sites. They are created with the add_shortcode() function, which takes a shortcode name and function. The function receives attributes, content, and name as arguments. Attributes are processed with shortcode_atts() to set defaults. Content appears between opening and closing tags. The name allows one function to handle multiple shortcodes. Shortcodes can be removed with remove_shortcode(). do_shortcode() processes shortcodes in content.
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. 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. 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
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. 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. 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. 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. 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. That’s It!
• Now go forth and write shortcodes!
• Any Questions?
Monday, November 7, 11