HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Custom BBCodes

R_JR_J Ex-FanboyMunich Admin

I have been asked how to create custom BBCodes and I've realized that I never saw anyone speaking about this/offering a solution.

Since it is really simple, here is an example together with some places to look for information.

If you are the "first I learn before I do something" type of coder, here is some food for yor brain:
And if you are more the "let me get my hands dirty, the sooner the better!" type of coder, here are two files where you can find BBCode implementations:

Even if you prefer looking at the files, you should take a quick glance on the documentation and read a little bit about validating the input so that you will not open security holes with your custom BBCodes.

Enough said, here is a small example plugin which adds a custom BBCode so that a [bgbox color="green"]Hooray![/box] will be shown as <span class="BGBox" style="background-color:green">Hooray!</span>


$PluginInfo['customBBCode'] = [
    'Name' => 'Custom BBCode',
    'Description' => 'Adds some custom BBCode tags.',
    'Version' => '0.1',
    'Author' => 'Robin',
    'RequiredApplications' => ['Vanilla' => '>=2.3'],
    'MobileFriendly' => true,
    'License' => 'MIT'

class CustomBBCodePlugin extends Gdn_Plugin {
    public function bbcode_afterBBCodeSetup_handler($sender, $args) {
        $nbbc = &$args['BBCode'];
                'mode' => $nbbc::BBCODE_MODE_ENHANCED,
                'template' => '<span class="BGBox" style="background-color:{$color}">{$_content}</span>',
                'allow' => ['color' => '/[a-zA-Z]+/'],
                'default' => ['color' => 'yellow'],
                'class' => 'inline',
                'allow_in' => ['listitem', 'block', 'columns', 'inline', 'link']

The BBCode rule added here is really simple. For more complex examples please look at the two files mentioned above. Have fun!



  • pitkmipitkmi scifi-meshes.com

    Any idea why this one wouldn't work on 3.3? I gave this one a try today, but it didn't do anything. I split $PluginInfo to addon.json, copied the php as is, but the span never shows up inline. The BBCode is getting parsed I think, since the [bgbox] tags aren't visible in the post but are present in the editor, but they're not affecting anything. Didn't see any errors anywhere.

    I have been adding custom BBCodes straight into the Core files, so it's certainly doable, but handling them as plugins would make updates a lot less likely to break things I forgot I changed.

  • R_JR_J Ex-Fanboy Munich Admin

    The problem is the HTML sanitizer. Even if you allow some tags in bbcode, they get stripped out afterwards. I found no clean solution for that. In theory you should be able to use the DI container to register your own class and allow some more tags, but I wasn't successful when trying to do so. There is another discussion about that. You might be able to find it and get more insights

  • how to insert a code snippet ?

  • KasparKaspar Moderator

    Code Example

Sign In or Register to comment.