Bringing Shortcodes to Drupal

Nov 28, 2011
by Ivan

One of the things I love the most from Wordpress is the ability to define custom shortcodes on your theme's functions.php. This is possible on Drupal, but as with most things with my favorite CMS, it is more powerful but requires more work to set it up.

Ingredients

Creating a Shortcode

Once you have Custom Filters enabled, go to the module page and add a new filter. Name it the way you want, and click save. Once you are back in the filters list, click on the one you just created so we can create a rule.

Custom Filter
When creating a rule, you have to provide a regular expression of the code you want to replace. In this case, we are going Wordpress style [shortcode] and we will just replace it with a image. We will use [checkmark] as our short code, so under Pattern add:

/\[checkmark]/i

Now, our image will be located in our theme folder and we won't be using PHP parsing, so we just insert:

<img src="/sites/all/themes/THEME/images/checkmark.png" />

into the replacement text box.

Custom Filter

Now, we just need to enable our custom filter in Text Formats options. Go to Text Formats, select the one you are currently using and under Enabled Filters, locate and select the one you just created.

Posted in