• This forum has been archived. New threads and replies may not be made. All add-ons/resources that are active should be migrated to the Resource Manager. See this thread for more information.

Using BB Codes: Tips & Tricks (Programmers Guide)


Well-known member
I'll call myself a self-addicted BBCode programmer on XenForo. Many of you may use BB Code Manager, but I'm slowly reading and learning new items about XenForos BB Code System. I suggest you read the comprehensive guide on creating BB Codes, as I will not be touching upon them here.

Setting up a Parser: Using "Base"

XenForo using a static function called create() within the XenForo_BbCode_Formatter_Base class. Normal usage is done as this:
$parser = new XenForo_BbCode_Formatter_Base::create('XenForo_BbCode_Formatter_Base', $optionsArray);
However, if I am creating "Base" we can use the following and get the same results:
$parser = new XenForo_BbCode_Formatter_Base::create();
Setting up a Parser: Using non-Base XenForo parsers

In addition, if I wanted to create an instance of XenForo_BbCode_Formatter_Text, I would use the following code (omitting the options here):
$parser = new XenForo_BbCode_Formatter_Base::create('XenForo_BbCode_Formatter_Text');
The create() method includes a small check to see if the $class parameter of create() (the first one) includes the underscore character ('_'). If it doesn't, it automatically adds, "XenForo_BbCode_Formatter_" to the beginning of $class. Meaning, the following code will allow you to set up a parser with less typing on your end:
$parser = new XenForo_BbCode_Formatter_Base::create('Text');
As I continue read-throughs of BB Code classes & functions, I will continue to expand this list. :) (Probably will be adding more tonight as well.)

Implementing an Image Count limit option

Thanks to ragtek for the initial tip, however, here's a more indepth (XenForo based) top on how to use XenForo_BbCode_Formatter_ImageCount to add a limit on the number of images and media within a single post in your add-on. This class extends XenForo_BbCode_Formatter_Base and makes 2 changes, ands 2 variables, and two functions. You gain a function to access each count variable added, getImageCount() and getMediaCount(), which can allow for your checking system. Here's an example using a fictional setting: myAddon_max_images_and_media.
$parser = new XenForo_Formatter_Base::create('ImageCount');
$count = XenForo_Application::get('options')->myAddon_max_images_and_media;
if(($parser->getImageCount() + $parser->getMediaCount()) > $count)
throw new XenForo_Exception(new XenForo_Phrase('myAddon_toomanymedia'), true);
Of course, these can be separated at will, but the idea is the same. You would of course handle the error in which ever way you choose.



this tutorial helped me a little bit and made me longing to know (hope that's the right meaning^^, that's what google translate showed for german "neugierig")...

nevermind, b2t:

it seems that the formatter is also great to validate the message, like xenforo does with the max. Image & Media check via XenForo_BbCode_Formatter_ImageCount
(for example to count the links / or to check for deadlinks).
          $formatter = XenForo_BbCode_Formatter_Base::create('Ragtek_LC_LinkCounter', false);
            $parser = new XenForo_BbCode_Parser($formatter);

            if ($formatter->getLinkCount() > 0 AND !$visitor->isAllowedToPostLink()) {
                $this->error(new XenForo_Phrase('ragtek_pmc_not_allowed_to_post_links'), 'message');

            if ($invalidLink = $formatter->searchDeadLinks()){
                $this->error(new XenForo_Phrase('ragtek_pmc_dead_link', array('link'  => $invalidLink), 'message'));
hope it's ok that i wrote this here^^ (if not, feel free to report this post so somebody deletes it^^)