New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

gornymedia/laravel-shortcodes

Laravel shortcodes package
43,965 23
Install
composer require gornymedia/laravel-shortcodes
Latest Version:v1.5
Laravel:11 · 12
Older releases:6 – 10
License:MIT
Last Updated:May 29, 2025
Links: GitHub  ·  Packagist
Maintainer: gornymedia

Laravel-Shortcodes

Laravel Shortcodes Package

Laravel compatibility

Laravel Shortcodes
12.x 1.5
11.x 1.5
10.x 1.4
9.x 1.4
8.x 1.4
7.x 1.4
6.x 1.4
5.8.x 1.3
5.5.x 1.2
5.4.x 1.1
5.3.x 1.1
5.2.x 1.1
5.1.x 1.0
5.0.x 1.0

Installation

Require this package with Composer

composer require gornymedia/laravel-shortcodes 1.5

Add the service provider to bootstrap/providers.php

Gornymedia\Shortcodes\ShortcodesServiceProvider::class,

To use the facades, add this within the register method of app/Providers/AppServiceProvider

$loader = \Illuminate\Foundation\AliasLoader::getInstance();
$loader->alias('Shortcode', \Gornymedia\Shortcodes\Facades\Shortcode::class);

Copy the package config to your local config with the publish command

php artisan vendor:publish --provider="Gornymedia\Shortcodes\ShortcodesServiceProvider"

Usage

Creating shortcodes

Inside an app/Providers/AppServiceProvider boot function you can create the shortcodes with attributes.

use Gornymedia\Shortcodes\Facades\Shortcode;

Shortcode::add('example', function($atts, $content, $name)
{
  $a = Shortcode::atts([
    'name' => $name,
    'foo' => 'something',
    ], $atts);
    
  return "foo = {$a['foo']}";
});

Usage : [example foo="something else"]

Include partial files in shortcode

use Gornymedia\Shortcodes\Facades\Shortcode;

Shortcode::add('widget', function($atts, $content, $name) 
{
  $a = Shortcode::atts([
    'name' => $name,
    'foo' => 'something'
  ], $atts);
  
  $file = 'partials/' . $a['name'] ; // ex: resource/views/partials/ $atts['name'] .blade.php
 
  if (view()->exists($file)) {
    return view($file, $a);
  }
});

Usage : [widget name="maps"]

Compile shortcodes inside shortcode content

use Gornymedia\Shortcodes\Facades\Shortcode;

Shortcode::add('strong', function($atts, $content, $name) {
 $content = Shortcode::compile($content);
 
 return "<strong>$content</strong>";
});

Usage: [strong][example][/strong]

View rendering

compileShortcodes()

To render the view and compile the Shortcodes:

return view('view')->compileShortcodes();

stripShortcodes()

To render the view and remove the Shortcodes

return view('view')->stripShortcodes();

In the config/gornymedia-laravel-shortcodes.php file you can set the default mode (null, compile or strip).

The following example will compile shortcodes without using the compileShortcodes() method

return [
    'mode' => 'compile'
];

License

This package is open-source software licensed under MIT License.

This package uses WordPress shortcodes methods. The license under which the WordPress software is released is the GPLv2 (or later).

Support

Support me with any amount and help me develop.

Related Packages

vedmant/laravel-shortcodes

Wordress like shortcodes for Laravel

13,834 27
webwizo/laravel-shortcodes

Wordpress like shortcodes for Laravel 11, 12 and 13

764,981 218
brouwers/shortcodes

Wordpress like shortcodes for Laravel 4.2

12,705 20
brouwers/bbcodes

BBcodes for Laravel

295 5

Version History

Version Released PHP Laravel License
v1.5 ^11.0 || ^12.0 MIT
v1.4 ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 MIT
v1.3 >=7.1.3 MIT
v1.2 >=5.4.0 MIT
v1.1 >=5.4.0 MIT