WordPress Shortcodes: A Complete Guide With Examples to Create Your Own

The Shortcode API is a simple set of functions for creating WordPress shortcodes for use in posts and pages.
Shortcodes are written by providing a handler function. Shortcode handlers are broadly similar to WordPress filters: they accept parameters (attributes) and return a result (the shortcode output).

Shortcode names should be all lowercase and use all letters, but numbers and underscores should work fine too. Be wary of using hyphens (dashes), you’ll be better off not using them.

The add_shortcode function is used to register a shortcode handler. It takes two parameters: the shortcode name (the string used in a post body), and the callback function name.

Three parameters are passed to the shortcode callback function. 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

Simple Shortcode

Leave a comment

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