O'Reilly logo

WordPress® All-in-One For Dummies® by Michael Torbert, Andrea Rennick, Kevin Palmer, Cory Miller, Lisa Sabin-Wilson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required