What’s Shortcode API?

144. What's Shortcode API.

The Shortcode API is a simple set of functions for creating WordPress shortcodes to use in posts and pages.

The API enables plugin developers to create special kinds of content which users can attach to certain pages by adding the corresponding shortcode into the page text.

The API manages all the tricky parsing, eliminating the need for writing a custom regular expression for each shortcode. Helper functions are also included for setting and fetching default attributes. The API supports both self-closing and enclosing shortcodes.

Shortcodes are written providing a handler function. Shortcode handlers are mainly similar to WordPress filters: they accept parameters and return a result.

Shortcode names need to be all lowercase and use all letters, but numbers and underscores should work fine too. You should be wary of using hyphen, you’ll be better off not using them.

The add_shortcode function is used for registering a shortcode handler. It takes two parameters: the shortcode name, and the callback function name.

Three parameters are passed to the shortcode callback function and you can choose to use any number of them including none of them.

  • $atts – an associative array of attributes, or an empty string if no attributes are given
  • $content – the enclosed content (if the shortcode is used in its enclosing form)
  • $tag – the shortcode tag, useful for shared callback functions

This much, guys! For more details you can look up in the WordPress Codex.

Share this!

Leave a Comment

Your email address will not be published. Required fields are marked *