A lightweight, template-based shortcode engine for Joomla 4 and later.
This plugin scans Joomla content (articles, categories, etc.) for shortcodes and replaces them with the output of corresponding PHP template files or callables. It's designed to be simple, fast, and easy for developers to extend.
- Simple Syntax: Use curly braces for shortcodes:
- self-closing:
{tag}or{tag attr="value"}. - with content between opening and closing tags:
{tag attr="value"}content{/tag}.
- self-closing:
- Template-Based: Each file-based shortcode corresponds to a simple PHP file. No database, no complex UI.
- Callable-Based: Define shortcodes directly using PHP functions for more complex logic.
- Developer-Friendly: Easily create new shortcodes by adding a PHP file or defining a callable.
- Content-Aware: Shortcode templates and callables have access to the current Joomla article (
$item). - Lightweight: Minimal processing overhead.
- Download the latest release package (a
.zipfile). - In your Joomla administrator panel, go to
System->Install->Extensions. - Upload the downloaded
.zipfile. - Enable the "Content - Shortcoder" plugin by going to
System->Manage->Plugins.
- Create a
shortcodesdirectory in the root of your Joomla installation (if it doesn't already exist). - Inside the
shortcodesdirectory, create a new PHP file for each shortcode you want to add. The filename (without.php) becomes the shortcode tag. For example,my_shortcode.phpwill be available as{my_shortcode}. - Write the PHP and HTML for your shortcode output in that file.
Example shortcodes/hello.php:
<?php
\defined('_JEXEC') or die;
$name = $attributes['name'] ?? 'World';
?>
<strong>Hello, <?php echo $name; ?>!</strong>
<p><?php echo $content; ?></p>You can then use it in your Joomla articles like this:
{hello name="John Doe"}This is the content inside the shortcode.{/hello}
This will be rendered as:
<strong>Hello, John Doe!</strong>
<p>This is the content inside the shortcode.</p>For more advanced shortcodes or when you prefer to keep logic within PHP code rather than separate template files, you can define callable shortcodes.
- Create a file named
shortcodes.phpdirectly inside yourshortcodesdirectory (e.g.,JPATH_ROOT/shortcodes/shortcodes.php). - This
shortcodes.phpfile must return an associative array where keys are the shortcode tags and values are PHP callables (functions, closures, static methods, etc.). - Callable shortcodes defined in
shortcodes.phpwill take precedence over file-based shortcodes with the same tag name.
Example shortcodes/shortcodes.php:
<?php
\defined('_JEXEC') or die;
return [
'hello' => fn (array $attributes) => sprintf('Hello, %s!', $attributes['name'] ?? 'World'),
'current_year' => fn() => (string) date('Y'),
];You can then use it in your Joomla articles like this:
{hello name="Alice"}
{current_year}
Within your shortcode template files or callable shortcodes, you have access to:
$attributes: An associative array of the attributes passed to the shortcode.$content: The string of content nested between the opening and closing shortcode tags.$item: The Joomla content item object (e.g., article, category) being processed.
Shortcode attributes can be defined in several ways:
-
Named Attributes (key="value"): These are the standard
key="value"pairs.- Double Quotes: Attributes enclosed in double quotes
{tag attr="value"}allow for values containing spaces and special characters. This is generally the recommended approach. - Single Quotes: Attributes enclosed in single quotes
{tag attr='value'}also allow for values containing spaces and special characters. - Unquoted: Attributes without quotes
{tag attr=value}are are allowed, but the value must not contain spaces or special characters. It will be parsed until the next space or the closing bracket.
- Double Quotes: Attributes enclosed in double quotes
-
Positional Attributes: These are values passed directly without a key, like
{tag 'value1' 'value2'}.- They are accessible in the
$attributesarray using numeric keys (0, 1, 2, ...). - Additionally, all positional attributes are collected into a special array accessible via the
'_'key in the$attributesarray (e.g.,$attributes['_'][0]). This is useful for iterating over all positional arguments.
- They are accessible in the
Examples:
{example unquoted=value single='single quoted' double="double quoted value with spaces" 'positional value 1' positional_value_2}
In your shortcode, $attributes would look something like this:
[
'unquoted' => 'value',
'single' => 'single quoted',
'double' => 'double quoted value with spaces',
0 => 'positional value 1',
1 => 'positional_value_2',
'_' => ['positional value 1', 'positional_value_2'],
]-
Nested Shortcodes: Nesting a shortcode within another shortcode of the same name is not supported due to limitations in the regular expression-based parser. For example, the following structure will not work as expected:
{my_shortcode} {my_shortcode}...{/my_shortcode} {/my_shortcode}However, nesting shortcodes with different names is fully supported:
{one} {two}...{/two} {one}
This project is licensed under the MIT License.