Shortcodes

Shortcodes in Latch are a convenient way to produce dynamic output in CMS-managed content, menu links, settings, and templates. They are powered by the Thunder library and use BBCode-style syntax.

The definition template of a shortcode is as follows:

<?php

namespace Latch\Shortcodes;

class MyShortcode extends Shortcode
{
    public function invoke($shortcode)
    {
        return "something";
    }
}

Core shortcodes live in lib/shortcodes. Extension shortcodes live in extensions/my_extension/lib/shortcodes.

The shortcode filename becomes the shortcode tag. For example, my-shortcode.php registers [my-shortcode]. The PHP class name must be the PascalCase form of the filename, so my-shortcode.php should define Latch\Shortcodes\MyShortcode.

The invoke() method is called when the processor encounters the shortcode. It receives a Thunder shortcode object, which exposes methods such as getBbCode(), getParameter(), getParameters(), hasParameter(), and getContent().

Shortcode Permissions

Shortcodes can include files and render CMS objects, so they should only be evaluated in trusted content. Latch evaluates shortcodes in administrator-managed content paths such as page content, menu item output, and selected settings.

Sanitize any user-provided values handled by custom shortcodes. Use \Latch\Input\sanitize() for scalar input and return escaped output when printing user-controlled text.

Included Shortcodes

Latch provides the following shortcodes out of the box.

[file]

Includes a Latch template file and returns its rendered output. This shortcode uses the normal extension override lookup, so an active extension can provide the requested file ahead of the core file.

[file="components/header.php"]

The documentation portal uses this shortcode to pull articles from docs/manual, for example:

[file="docs/manual/shortcodes.md"]

Markdown files resolved through Latch's file loader are rendered as Markdown automatically.

[form]

Renders a frontend form by form name or ID.

[form="Contact"]
[form=17]

The shortcode wraps the rendered fields in a submit-ready <form> element.

[icon]

Renders a Font Awesome icon.

[icon="house"]
[icon="github" class="fab"]

If class is omitted, Latch chooses a known Font Awesome prefix for the icon when possible and falls back to fa.

[markdown]

Converts Markdown to HTML using Latch's Markdown parser.

[markdown="# Heading"]
[markdown file="docs/manual/forms.md"]

Use the file parameter only with trusted paths.

Renders a menu by name.

[menu="Primary Navigation"]

Menu link and content fields are also passed through shortcode evaluation, which makes shortcodes such as [site], [path], and [icon] useful inside menus.

[path]

Produces a URL for a Latch file or CMS route.

[path="components/header.php"]
[path="my-page"]
[path=20]

If the value has a file extension, Latch resolves it through Latch\File\ext_part_url(), prioritizing active extensions ahead of core files.

If the value has no file extension, Latch treats it as a friendly URL and returns LATCH_ROOT plus the path slugified one segment at a time. Slash-separated paths therefore retain their hierarchy. If the value is numeric, Latch returns the post's configured friendly permalink when available and otherwise returns its direct view-page URL.

[post]

Renders a single post by post ID or exact title.

[post=20]
[post="About Us"]
[post=20 include_title]
[post=20 include_header="true"]

The rendered post uses the same post template resolution as normal page rendering. The shortcode sets the wrapper class to shortcode-post fullwidth.

[posts]

Renders a list of posts using each post's template, with the same rendering path as [post].

  • types / type / forms / form / post_types / post_type: Comma-separated list or JSON array of form names or IDs.
  • limit (int): Maximum number of posts to return. Defaults to the Layout -> Post Search Limit setting, or 20 if the setting is empty.
  • offset (int): Result offset.
  • orderby / order_by / sort_by / sort: Sort column. Allowed database values are id, date_published, and date_updated.
  • order / direction: Sort direction, either ASC or DESC.
  • template / template_override (optional): Override the post template file for all results. This looks for posts/<template>.php.

Examples:

[posts types="Blog,News" limit="5" orderby="date_published" order="DESC"]
[posts post_types="[\"Blog\", \"Announcement\"]" limit="3" template="post-card"]
[posts=Blog,News limit="2" orderby="date_updated" order="ASC"]

Post results are filtered by the current session's read permissions.

[setting]

Returns the value of a database setting.

  • category (string): The category of the setting. Defaults to Identity.
  • name (string): The name of the setting. Defaults to Name.
  • default (string) (optional): The value to return if the setting is not found. Defaults to Not Found.
[setting category="Identity" name="Site Title"]

Possible output:

My Site Name

[site]

Returns the relative path to the root of your Latch app.

This is useful in menus and other settings where you want internal links to keep working across environments.

[site]/my-page

Possible output:

/latch/my-page

The absolute positional parameter returns LATCH_ADDRESS, which is the filesystem path to the Latch installation:

[site absolute]

[site-title]

Returns the Identity -> Site Title setting.

[site-title]

[str]

Returns a localized string from the Strings setting group.

[str="Welcome Message"]
[str="Welcome Message" lang="es"]

If the requested language is not available, Latch returns the first configured variation for that string.