Shortcodes provide a way for WordPress users to easily add dynamic content and functionality.
To properly use shortcodes, it is important to understand how they work.
Types of Shortcodes
There are two types of shortcodes.
1. Self-encapsulating: This means the shortcode uses only a single set of brackets.
2. Wrapping shortcodes: This means there are two sets of brackets; One before some type of content, and one after, used as a closing tag.
[restricted content]This is content that is restricted to members[/restricted content]
Shortcodes may have available attributes, or options that can be used to change how the shortcode works.
In this example…
…we are using the attribute, “formatted”, with the value of “false” to tell the function not to format the phone number. This particular shortcode will format the number by default unless told otherwise.
If we actually run the shortcodes we will see output like this:
Why Use Shortcodes?
- Consistency: Using a shortcode provides a consistent method to ensure data it outputs is consistent.
- Maintainability: Because of the dynamic nature of shortcodes, it is far easier to maintain. In the phone number example used above, if the phone number ever needs to be changed, it can be done in seconds just by updating a site setting. Without it, it would be necessary to locate and update each and every instance of the number appearing across the site, which could take a very long time and potentially lead to errors and omissions.
Keep in Mind
- A shortcode must be able to support what you’re trying to do. It must exist and if trying to use attributes, it must know how to handle the attributes and values used within it. Using our phone number example, you could not do something like
[get_oda_phone formatted=international]because the value “international” has not been coded to be interpreted by the shortcode function.