jaybizzle/shortcodes

BBCode/Wordpress style shortcodes

v1.1.2 2022-11-16 15:21 UTC

This package is auto-updated.

Last update: 2024-03-16 18:06:04 UTC


README

68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f4a617942697a7a6c652f53686f7274636f6465732f6d61737465722e7376673f7374796c653d666c61742d737175617265 68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f4a617942697a7a6c652f53686f7274636f6465732e7376673f7374796c653d666c61742d737175617265 68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f4a617942697a7a6c652f53686f7274636f6465732e7376673f7374796c653d666c61742d737175617265 68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d6666363962342e7376673f7374796c653d666c61742d737175617265 68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a617962697a7a6c652f53686f7274636f6465732e7376673f7374796c653d666c61742d737175617265 68747470733a2f2f7374796c6563692e696f2f7265706f732f39353539383934382f736869656c64 68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f4a617942697a7a6c652f53686f7274636f6465732f6d61737465722e7376673f7374796c653d666c61742d737175617265

Shortcodes

Shortcodes is a PHP library that will help parse WordPress/BBCode style shortcodes. It can turn something like this...

[style color=#FF0000]Red Text[/style]

into this...

<span style="color:#FF0000;">Red Text</span>

The output is not predefined, it is up to you to define how the output is handled. See below for examples.

Installation

composer require jaybizzle/shortcodes

Getting started *** (WIP) ***

Let's take a simple example. We want to create a Shortcode for video elements. We want to be able to write something like this...

[video title="My Awesome Video" videoID=345 width=320 height=240]

and make it output something like this...

<video width="320" height="240" controls>
    <source src="/videos/video-345.mp4" type="video/mp4">
    <source src="/videos/video-345.ogg" type="video/ogg">
    Your browser does not support the video tag.
</video>

Firstly, we need to create a class that is going to handle the parsed Shortcode and its attributes. We create a new class as follows...

<?php

namespace App\Shortcodes;

use Jaybizzle\Shortcodes\Shortcode;

class VideoShortcode extends Shortcode
{
    public static $shortcode = 'video';

    public function parse()
    {
        // All shortcode attributes will be available in $this->attr
        // i.e. given the example above...
        // $this->attr['title']
        // $this->attr['videoID']
        // $this->attr['width']
        // $this->attr['height']
    }
}

Next, we need to add this Shortcode parse class to the Shortcodes library like this...

<?php

namespace App\Libraries;

use App\Shortcodes\VideoShortcode;
use Jaybizzle\Shortcodes\Shortcodes;

class MyClass
{
    public function index()
    {
        $shortcodes = new Shortcodes
        $shortcodes->add(VideoClass::class);
    }
}