WordPress Shortcodes in CFML

If you've used WordPress, you may have run across shortcodes.  They're little directives you type into the editor box which then evaluate dynamically when the content is rendered on the outside of you site.  Plugins can register new shortcode handlers, which are implemented as a simple function callback.  It's a really simple way to expose psuedo-programming constructs to content authors in a safe manner (because you as the admin/developer can control which shortcodes are available), and without requiring any PHP knowledge and/or server access.

I needed this sort of functionality in CFML, so after playing with a few different syntaxes and parsers for them, I decided that a direct port of the WordPress shortcodes implementation was the best choice.  The code is pretty small (the grammar is context free and the parser is RegEx-based), and the port (including unit tests) took perhaps an hour and a half.  I had to roll my own REReplaceCallback UDF to match one of the PHP builtins, as well as change the callback API slightly to deal with CFML idoms, but it's a pretty direct port.

So what can you do with shortcodes?  Here's a little demo, both of the front side (the content) and the backside (the handlers and processing).  There is also a link to the source, of course.  And like all my projects, there is a project page where current project information will always be available.

3 responses to “WordPress Shortcodes in CFML”

  1. Aaron Terry

    Looks pretty sweet. I've been digging the Wordpress shortcode functionality and I might need this in a future CF project.

  2. Thomas Burleson

    Wordpress is an fantastic CMS with incredible versatility. And ShortCodes are amazing.
    Really like your demo of Latex ShortCodes.
    If you did a ShortCode for Syntax Highlighting for CFM pages that would be very cool.

    ThomasB