Documentation

Reusable Content & Text Blocks WordPress Plugin

Custom Variables 2017-11-24T10:35:22+00:00

Custom Variables

The plugin allows you to define custom variables, which can then be displayed within content blocks.

Setting Variables

To set a variable, you need to include it in the calling shortcode (e.g. within your page or post) or in the calling PHP code.

The basic syntaxes in shortcodes are:

[ls_content_block id="<ID>" para="<PARA>" <VARIABLE 1 NAME>="<VARIABLE 1 VALUE>" <VARIABLE 2 NAME>="<VARIABLE 2 VALUE>"]
[ls_content_block slug="<SLUG>" para="<PARA>" <VARIABLE 1 NAME>="<VARIABLE 1 VALUE>" <VARIABLE 2 NAME>="<VARIABLE 2 VALUE>"]

With variables passed as an associative array, the basic syntaxes in PHP are:

<? echo ls_content_block_by_id( <ID>, '<PARA>', array( '<VARIABLE 1 NAME>' => '<VARIABLE 1 VALUE>', '<VARIABLE 2 NAME>' => '<VARIABLE 2 VALUE>' ) ); ?>
<? echo ls_content_block_by_slug( '<SLUG>', '<PARA>', array( '<VARIABLE 1 NAME>' => '<VARIABLE 1 VALUE>', '<VARIABLE 2 NAME>' => '<VARIABLE 2 VALUE>' ) ); ?>

Notes:

  • Variable names must be lowercase alphanumeric (a-z, 0-9) and must begin with “var” (e.g. “varpercentage”, “varcountry20”, etc.).
  • In these examples, we have shown 2 variables, but you may use one or more.
  • Variable values may not contain an inverted comma (“) as WordPress will confuse this with the variable value delimiter.

Displaying Variables

Once defined, a content block (or one down a chain) can display the value of a variable within its content by using the shortcode:

[ls_content_block getvar="<VARIABLE NAME>"]

Notes:

  • After being set, variable values persist until a page or post is fully rendered unless they are overwritten. As such, once set, they may be called at any point after – whether in a content block or the post itself.
  • To avoid variable values causing potential issues within the final HTML of the page, they are HTML escaped before being displayed.