webwizo/laravel-shortcodes
Add WordPress-style shortcodes to any Laravel app. Register custom shortcode classes, then use them in Blade views, content, or emails. Supports attributes, self-closing and enclosing tags, with a clean API for parsing and rendering dynamic content.
^1.0.34 (or the package’s equivalent constraint), then run composer update.compile() entry point and shortcode registration API.parseAttributes() if you need direct attribute-parsing behavior.HtmlEntityAttributeParsingTest for the expected numeric-entity behavior.[button url="..." label="..."], then compile stored content before rendering:Shortcode::register('button', function (array $attrs, ?string $content = null) {
$url = $attrs['url'] ?? '#';
$label = $attrs['label'] ?? 'Button';
return sprintf(
'<a class="btn" href="%s">%s</a>',
e($url),
e($label)
);
});
{!! Shortcode::compile($post->body) !!}
compile() as the rendering boundary. Store shortcode markup in the database, compile at render time, and cache the rendered result only when the shortcode registry and content are stable.$url = filter_var($attrs['url'] ?? '', FILTER_VALIDATE_URL) ?: '#';
$target = in_array($attrs['target'] ?? '', ['_blank', '_self'], true)
? $attrs['target']
: '_self';
= and " parse correctly.$input = '[button url="https://example.com?a=1&b=2" label="Go"]';
$html = Shortcode::compile($input);
$this->assertStringContainsString('href="https://example.com?a=1&b=2"', $html);
parseAttributes() rather than relying on 0, 1, etc.{!! !!} only after ensuring handlers escape user-controlled attribute values. Prefer a dedicated helper or Blade directive so views do not call the compiler inconsistently.htmlspecialchars_decode() alone does not decode numeric entities like = and ". v1.0.34 fixes this inside the package’s attribute parser, so pin ^1.0.34 if your content passes through sanitizers.htmlspecialchars_decode() over the whole content. That can reintroduce encoded HTML and create XSS risks. Let the package parse shortcode attributes, or decode only targeted attribute values before escaping on output.e() or your templating engine when building HTML.=, ", and & differently. Cover =, ", and entity-encoded ampersands in URLs.HtmlEntityAttributeParsingTest exercises both parseAttributes() and the full compile() flow; mirror that in your application’s integration tests.How can I help you explore Laravel packages today?