Shortcode API
Shortcodes allow content authors to embed dynamic, plugin-generated content blocks inside post/page content using a simple bracket syntax: [my_shortcode attr="value"].
📍 Source: App\CMS\Shortcodes\ShortcodeManager
Registering a Shortcode
use App\CMS\Shortcodes\ShortcodeManager;
public function boot(): void
{
ShortcodeManager::register('announcements_list', function (array $attrs) {
$count = $attrs['count'] ?? 5;
$announcements = \App\CMS\Models\Post::where('post_type', 'announcement')
->where('status', 'published')
->latest()
->take($count)
->get();
return view('plugins.postify-announcements.views.shortcode-list', [
'announcements' => $announcements,
])->render();
});
}
Method Signature
ShortcodeManager::register(string $tag, callable $callback): void
| Parameter | Type | Description |
|---|---|---|
$tag | string | The shortcode tag name (e.g., 'announcements_list') |
$callback | callable | Receives array $attrs and optional string $content. Must return HTML string. |
Usage in Content
Content authors use the shortcode tag inside posts/pages:
<!-- Simple usage -->
[announcements_list]
<!-- With attributes -->
[announcements_list count="3"]
<!-- With enclosing content -->
[highlight color="yellow"]This text is highlighted[/highlight]
Enclosing Shortcodes
For shortcodes that wrap content, the callback receives a second $content parameter:
ShortcodeManager::register('highlight', function (array $attrs, ?string $content = null) {
$color = $attrs['color'] ?? 'yellow';
return "<span style=\"background:{$color}\">{$content}</span>";
});
How Shortcodes Are Processed
The ShortcodeManager::process() method is called automatically by the core when rendering post content. It parses all registered [tag] patterns and replaces them with the callback's output.
// This happens internally in the CMS core:
$renderedContent = ShortcodeManager::process($post->content);
💡 Best Practice: Return rendered Blade views from shortcode callbacks instead of concatenating HTML strings. Use view('...')->render() for clean, maintainable templates.
Last verified against AIPostify core. © 2026 AIPostify Marketplace Developer Documentation.