Creating Shortcodes
You can use the the_content filter to add new sections of content to posts and pages. For example, you can add a form for subscribing to new posts. However, this type of all-or-nothing method of adding content often adds content where it isn't wanted. Using a shortcode is often a much better solution because it offers flexibility and ease of use.
Shortcodes, introduced in WordPress 2.5, can be employed for a wide variety of uses. WordPress includes a shortcode, [gallery], which you can add to any post to display a gallery of images assigned to that post in place of the shortcode. (See Book IV, Chapter 4 on adding photo galleries to you posts.) Many plugins (such as a forum or contact form plugin) use shortcodes to allow the plugin users to designate specific pages for the plugin front end to appear. They can surround sections of content, allowing the code that powers the shortcode to modify the content.
The main features of a shortcode include
- A shortcode can stand by itself. The [gallery] shortcode (built into WordPress) is a good example of this; simply adding [gallery] to the content of a page or post is all that is needed to allow the shortcode to insert a gallery of uploaded images and display them within the body of a post or page.
- A shortcode can support arguments that pass specific information to the code that powers the shortcode. For example, [gallery width=”400” caption=”My Venice Vacation”].
- Shortcodes can be used around a section of content the ...
Get WordPress® All-in-One For Dummies® now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.