A Guide to Using Shortcodes

This Article Applies to

In this article we’re going to take you from Shortcode Beginner to ShortCode Ninja. If you find yourself confused, or lost when reviewing this, reach out to us at support for assistance.

Basics
What Are Shortcodes?
Shortcode Attributes

Pro Tips
Conditional Shortcodes
How to Nest Shortcodes

Ninja Tips
Formatting Your Output
Local Variables
Capturing Your Output for Later
Using Shortcodes in your PHP code

The Other Stuff
Changes in WordPress Shortcodes
Troubleshooting Shortcodes

What Are Shortcodes?

Shortcodes are powerful little commands that you can use in your pages to make your site do different things. WordPress comes with a set of built-in shortcodes, and most themes and plugins add their own shortcodes as well.

Memberium provides you a powerful toolbox of shortcodes for creating, automating and making your membership site interactive.

WordPress shortcodes look similar to HTML tags, consisting of a phrase surrounded by a set of square brackets []. A shortcode can take parameters, which we often call “attributes”. A shortcode can stand by itself, or it can consist of an opening and closing shortcode and contain content. We’ll show you simple examples of all these below:

All Memberium shortcodes are in all lowercase, and start with “memb_”. The following are some examples of how shortcodes are used, you can try these examples on your own site to see how they work:

A simple shortcode by itself:

[memb_appname]

A simple shortcode, with attributes:

[memb_echo value='Hello, World']

A shortcode containing content with opening and closing codes:

[memb_is_admin]
Only show this to admins
[/memb_is_admin]

A shortcode with attributes and content:

[memb_has_any_tag tagid=123]
Only show this to people with tag #123
[/memb_has_any_tag]

Shortcode Attributes

Shortcode attributes allow one shortcode to do many different things. For example, Memberium has one shortcode for displaying the fields of the currently logged in Infusionsoft contact. The attributes let you tell the [memb_contact] shortcode which of the fields to display, and how to display it. The names of shortcode attributes are always all in lowercase.

Example:

[memb_contact fields=FirstName]

IF the value of your attribute has spaces in it, then you’ll need to enclose it in quotes so that WordPress knows where your value starts and finishes. You can use single quotes, or double quotes.

[memb_echo value="Hello, World"]
[memb_echo value='Hello, World']

If you have several attributes, it doesn’t matter what order they are used in. These examples work exactly the same:

[memb_echo value='Hello, World' txtfmt=strtoupper]
[memb_echo txtfmt=strtoupper value='Hello, World']

Conditional Shortcodes

Conditional shortcodes do a test and then allow you to choose what content to display depending on what the results of the test were. Tests always result in a yes/no, true/false, success/failure result. Conditional shortcodes usually contain a word like “is” or “has”, indicating a condition. All tests are simple true-false / sucess-fail / yes-no tests.

Conditional shortcodes can be used three different ways:

You can use the shortcode by itself. It will displays “Yes” if the shortcode test passes, or “No” if it fails.

Example:

[memb_is_logged_in]

With many shortcodes you can use the attribute “output” to display something instead of “Yes”.

Example:

[memb_has_any_tag tagid=123 output='It Worked!']

Often you want to control a large amount of content. You can control whether a block of content is dislpayed by wrapping your shortcode around it. The content in the shortcode is displayed if the test passes, and not displayed if the test fails. The content blocks can be as large as you want. To create a closing shortcode use “/” at the beginning of the closing shortcode.

Example:

[memb_is_logged_in]
You are Logged In
[/memb_is_logged_in]

Sometimes you will want to display content if the test fails, you can do this by wrapping your shortcode around two blocks of content, one for a successful test, and one for a failed test, and separating them with the [else_memb_*] shortcode.

Example:

[memb_is_logged_in]
You are Logged in
[else_memb_is_logged_in]
You are not logged in
[/memb_is_logged_in]

Example with no “success” content:

[memb_is_logged_in][else_memb_is_logged_in]
You are not logged in
[/memb_is_logged_in]

Nestable Shortcodes

Sometimes you need to do multiple tests and have them all done to decide whether to decide to display the content or not. Placing one shortcode inside another is called nesting. Most conditional shortcodes are nestable.

If the second test follows after the first test completes, that is not nesting, and you don’t need to use the nesting ability. You can have as many tests on the page as you want. You only need to nest them if the next test starts before the previous test finishes.

There is a predetermined number of times that any given shortcode can be nested.

To nest a shortcode simply add a number to the end of the shortcode name. Similarly for [else_ and closing shortcode markup, you would add the number on as well. By default you can have up to 10 levels deep, shortcode and shortcode1…shortcode9. If you DO need to nest shortcodes deeper than 10 levels, please contact support.

Some examples

[memb_is_logged_in]
[memb_is_logged_in1]
[memb_is_logged_in2]
[else_memb_is_logged_in2]
[/memb_is_logged_in2]
[else_memb_is_logged_in1]
[/memb_is_logged_in1]
[else_memb_is_logged_in]
[memb_is_logged_in1]
[else_memb_is_logged_in1]
[/memb_is_logged_in1]
[/memb_is_logged_in]
[memb_is_logged_in]
[memb_is_logged_in1]
[else_memb_is_logged_in1]
[/memb_is_logged_in1]
[/memb_is_logged_in]

Text Formatting

Most Memberium shortcodes also support passing a txtfmt parameter that lets you specify a comma delimited list of text processing functions that will be applied on the output. If you use txtfmt and capture together, the text formatting will be applied first and then the result will be captured. You can stack functions together and they will be applied in order from first/left to last/right. You can stack as many of these text formatting codes together as you wish.

addslashes
Quote string with slashesbin2hex
Convert binary data into hexadecimal representation. The conversion is done byte-wise with the high-nibble first.convert_uudecode
Decodes the output which must be a uuencoded string.

convert_uuencode
Encodes the output using the uuencode algorithm. Uuencode translates all strings (including binary’s ones) into printable characters, making them safe for network transmissions. Uuencoded data is about 35% larger than the original.

crc32
Outputs the crc32 polynomial of the content.

crypt
Performs a one-way string hashing on the output. Caution: No salt is used.

html_entity_decode
Convert all HTML entities to their applicable characters.

htmlentities
Convert all applicable characters in the output to HTML entities.

htmlspecialchars
Convert special characters to HTML entities.

htmlspecialchars_decode
Convert special HTML entities in the output back to characters.

lcfirst
Make the output’s first character lowercase.

ltrim
Strip whitespace from the beginning of the output.

md5
Returns the md5 hash of the output.

nl2br
Inserts HTML line breaks before all newlines in a string

remove_accents
Converts all accent characters to ASCII characters. If there are no accent characters, then the output is unaltered.

rtrim
Strip whitespace from the end of the output.

sha1
Outputs the SHA1 hash of a string.

str_rot13
Outputs the ROT13 encoding of a string. The ROT13 encoding shifts every letter by 13 places in the alphabet while leaving non-alpha characters untouched. Encoding and decoding are done by the same function, passing an encoded string as argument will return the original version.

stripslashes
Un-quotes a quoted string.

strlen
Returns the length of the output. The length is measured in bytes.

strrev
Reverses the output.

strtolower
Make the output lowercase.

strtoupper
Make the output uppercase.

ucfirst
Make a string’s first character uppercase.

trim
Strip whitespace (or other characters) from the beginning and end of a string.

ucwords
Uppercase the first character of each word in a string. The definition of a word is any string of characters that is immediately after a whitespace (These are: space, form-feed, newline, carriage return, horizontal tab, and vertical tab).

wptexturize
This returns given text with transformations of quotes to smart quotes, apostrophes, dashes, ellipses, the trademark symbol, and the multiplication symbol. Text enclosed in the tags pre code, kdb, style, script and tt will be skipped.

The following example outputs the first name, converted to all lowercase

[memb_contact fields=FirstName txtfmt=strtolower]

The following example outputs the first name, converted to all lowercase, and then the first letter uppercase’d.

[memb_contact fields=FirstName txtfmt=strtolower,ucwords]

Local Variables

Sometimes you need to change the values you’re comparing or using in your shortcodes on the fly, so that they’re different depending on the conditions.

Memberium is able to store data about the current visitor in the local WordPress database, instead of Infusionsoft. This provides essentially unlimited fields (limited only by memory and disk space) for storing additional information.

You can capture data into local variables, you can display those variables to the screen, and you can use those variables in the attributes of Memberium shortcodes.

Local variables can be embedded into attributes by wrapping the variable in double curly braces “{{}}”. We support several kind of variables and some of those variables have different formats, and we may extend those formats as we make the system more capable. Here’s some examples:

To display fields from your visitor’s contact data, use the “contact.” followed by the name of the field. Contact fields are not case sensitive:

{{contact.firstname}}
{{contact.lastname}}
{{contact._mycustomfield}}

Using information from a contact’s affiliate record:

{{affiliate.id}}
{{affiliate.code}}

You can use information about the server’s current date/time, and format it as you need.

Using the date in Year Month Day format:

{{date.Y-m-d}}

Using the current server time in hour format:

{{date.h}}

Custom Fields:

{{field.fieldname}}

The visitor’s IP Address:

{{ip_address}}

Random numbers:

{{random.MINVALUE,MAXVALUE}}

GET/POST/Session Values:

{{post.key1}}
{{post.key1,key2,...}}
{{get.key1}}
{{get.key1,key2,...}}
{{session.key1}}
{{session.key1,key2,...}}

Miscellaneous:

<<::

outputs

[
::>>

outputs

]

Capturing Your Output

Most Memberium shortcodes let you control what is done with the output of your shortcodes. Under normal use, the shortcode is simply displayed to the browser. However, you can also save the output for later by capturing it into a custom local variable. you pass a shortcode attribute of “capture” with a comma separated list of destinations to send the output of the shortcode. It does not matter what order the destinations are listed in

Current parameters you can use in the capture parameter are:

display – Displays the output as usual. If you have capture specified and omit this, then the normal display of the shortcode won’t happen.

field:fieldname – Captures the output of the shortcode into the custom local variable field stored in WordPress.

Example, capturing first and lastname without displaying it:

[memb_contact fields="FirstName,LastName" capture="field:FullName"]

Example, capturing the output AND displaying it

[memb_contact fields="FirstName,LastName" capture="display,field:FullName"]

Using Shortcodes in your PHP Code

Shortcodes are automatically processed when used in Menus, Widgets and Page/Post content. If you wish to use the shortcodes in your PHP code, you can easily do this using WordPress’s do_shortcode function.

Example:

<!--?php echo do_shortcode('[memb_contact fields=FirstName]'); ?-->

If you’re doing a conditional shortcode, the entire shortcode needs to be processed at once.

Example:

<!--?php echo do_shortcode('[memb_has_any_tag tagid=123]Success[else_memb_has_any_tag]Failure[/memb_has_any_tag]'); ?-->

Troubleshooting Shortcodes

In some situations you may find that shortcodes stop working, or show up oddly on the page. This is easily fixable.

OptimizePress
If you are running OptimzePress 2.0 and only parts but shortcodes are visible up to the quotes and unprocessed on your site, then you may be hitting an issue with how OptimizePress stores your content. You can easily work around this by replacing the double quotes in your shortcode with single quotes, or by using no quotes at all as long as there are no spaces in your parameters.

  • Was this Helpful?
  • YesNo
9 ways to add more value to your membership site

Table of Contents

This Article Applies to

Keep Reading

Want to get some fresh ideas on how you can improve your membership site or course?

Download our free ebook!

Book a Call

Welcome to Memberium!

We are very excited for you to be part of our family. 

We would love to answer any questions that you have!

Please choose the best time for you to get in a call with us. 

For Technical Support, you can contact us at https://keap.memberium.com/support/ or Email us at support@memberium.com.