Mastering the shortcode api
-
Upload
peter-baylies -
Category
Technology
-
view
2.556 -
download
1
description
Transcript of Mastering the shortcode api
![Page 1: Mastering the shortcode api](https://reader036.fdocuments.in/reader036/viewer/2022081816/5470a3f3b4af9fc80a8b4832/html5/thumbnails/1.jpg)
Mastering the Shortcode API
Peter Baylies
Semper Fi Web Design
Monday, November 7, 11
![Page 2: Mastering the shortcode api](https://reader036.fdocuments.in/reader036/viewer/2022081816/5470a3f3b4af9fc80a8b4832/html5/thumbnails/2.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081816/5470a3f3b4af9fc80a8b4832/html5/thumbnails/3.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081816/5470a3f3b4af9fc80a8b4832/html5/thumbnails/4.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081816/5470a3f3b4af9fc80a8b4832/html5/thumbnails/5.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081816/5470a3f3b4af9fc80a8b4832/html5/thumbnails/6.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081816/5470a3f3b4af9fc80a8b4832/html5/thumbnails/7.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081816/5470a3f3b4af9fc80a8b4832/html5/thumbnails/8.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081816/5470a3f3b4af9fc80a8b4832/html5/thumbnails/9.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081816/5470a3f3b4af9fc80a8b4832/html5/thumbnails/10.jpg)
That’s It!
• Now go forth and write shortcodes!
• Any Questions?
Monday, November 7, 11