Skip to content

JayBizzle/Shortcodes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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);
    }
}