I want to add a new CMS directive to render CDN media URL's just like the media directive:

{{media url="wysiwyg/image.jpg"}}

Will result in http://www.example.com/media/wysiwyg/image.jpg

Now I have some specific images that will be hosted on a separate CDN. I want to be able to render URL's to those images from CMS pages/blocks:

{{cdnmedia url="images/picture.jpg"}}

This should result in http://cdn.example.com/images/picture.jpg where the CDN base URL would be configurable.

I already have a helper that can take "images/picture.jpg" as input and will return a full CDN URL (based on configured secure and unsecure CDN bas URL's).

How can I add this into the CMS rendering?

2 Answers 2


This is how I've done it right now.

Overriding CMS template filter

The CMS Helper contains two methods called getPageTemplateProcessor() and getBlockTemplateProcessor() which will do Mage::getModel(...) with input of the configured model name from the config respectively at global/cms/page/tempate_filter and global/cms/block/tempate_filter.

So add this to your module's config.xml to replace the original values of widget/template_filter (set in app/code/core/Mage/Widget/etc/config.xml):

    <!-- ... -->


  1. notice the node name <tempate_filter> is missing the 'l' in 'template' which is a typo in Magento core
  2. This is a form of rewrite. The last loaded module will do this rewrite effectively, therefore also it would be good practice to make your module <depends> on <Mage_Widget />

Custom template filter model with 'cdnmedia' directive

And the model:

class MyModule_Cdn_Model_Template_Filter extends Mage_Widget_Model_Template_Filter
     * CDN media URL filter
     * @param array $construction
     * @return string
    public function cdnmediaDirective($construction)
        $params = $this->_getIncludeParameters($construction[2]);

        if (!isset($params['url']) || '' == trim($params['url'])) {
            return '';

        $secure = null;
        if (isset($params['secure'])) {
            $secure = in_array($params['secure'], array('1', 'true', 'on', 'yes'));

        return Mage::helper('mymodule_cdn')->getUrl($params['url'], $secure);

Rendering the actual URL

The question is how to add the directive, not the actual URL creation, but for completeness, here's the 'mymodule_cdn' helper getUrl() method:

 * Get CDN URL for path
 * @param string $path
 * @param null|boolean $secure
 * @return string
public function getUrl($path = '', $secure = null)
    if ($secure === null) {
        $secure = intval(Mage::getStoreConfig('mymodule_cdn/default/is_secure')) > 0;
    return rtrim(($secure === true
            ? Mage::getStoreConfig('mymodule_cdn/secure/base_url')
            : Mage::getStoreConfig('mymodule_cdn/unsecure/base_url')
        ), '/')
        . '/' . ltrim($path, '/');


You can use the {{cdnmedia}} directive now in your CMS pages and static blocks:

<img src="{{cdnmedia url="images/picture.jpg" secure="true"}}" />
<!-- will render: -->
<img src="https://cdn.example.com/images/picture.jpg" />

And because of the use of a helper building the actual URL, you can use this in phtml templates:

<img src="<?php echo Mage::helper('mymodule_cdn')->getUrl('images/picture.jpg', true); ?>" />
<?php /* will render: */ ?>
<img src="https://cdn.example.com/images/picture.jpg" />


Used this article as reference: http://de.nr-apps.com/blog/2013/01/16/magento-store-url-template-directive/

  • Why you rewrite the filter in this way, instead of using <rewrite> node?
    – sv3n
    Jun 12, 2018 at 17:41
  • Where should a do a rewrite? I want to add a template directive, not change one. And also I always learned rewrites are bad, because they will inevitably lead to rewrite conflicts (when other exts/mods/code also needs to rewrite the same)
    – 7ochem
    Jun 13, 2018 at 7:46
  • Sorry, my bad. I misunderstood The is a form of rewrite.
    – sv3n
    Jun 13, 2018 at 12:52
  • Ah, I get it now. I guess this was an early form of DI already :P
    – 7ochem
    Jun 14, 2018 at 9:28
  • Maybe, just maybe in some cases you won't need to create new directive. You can try to use modifier! aleron75.com/2017/01/28/modifiers-in-magento-e-mail-templates Jan 25, 2019 at 17:32

You can find code in below url, how you can define your custom directives to use in Magento CMS pages & Static Block.


I have used this method before for me & its worked well for me.

  • Aha, very nice. I'm going to try this right away
    – 7ochem
    Apr 13, 2015 at 9:01
  • I've put my code in a separate answer. Thanks for pointing me into the right direction!
    – 7ochem
    Apr 13, 2015 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.