14

What is the equivalent of {{media url="wysiwyg/image.jpg"}} which can be used in CMS pages if we want to use this in a normal .phtml file (of the type core/template)

2 Answers 2

14

The rendering of this such URLs is done when displaying CMS pages or blocks in

\Mage_Core_Model_Email_Template_Filter::mediaDirective:

public function mediaDirective($construction)
{
    $params = $this->_getIncludeParameters($construction[2]);
    return Mage::getBaseUrl('media') . $params['url'];
}

Base on this code, just an

<img src="<?php echo Mage::getBaseUrl('media') ?>image.jpg">

can be used in the template

2
  • 2
    Just remove the blockquote(`) from the second code snippet (after ?>image.jpg">). (I can't edit less than 6 letters) Sep 21, 2013 at 8:13
  • 2
    +1 But it may be better to pass Mage_Core_Model_Store::URL_TYPE_MEDIA instead of 'media'? Nov 11, 2014 at 21:04
12

In a phtml template file:

Mage::getBaseUrl('media') . $url;

In a CMS block or page:

{{store direct_url="media/url"}}

The difference between {{media url="foo"}} and {{store direct_url="media/foo"}} is that the latter hardcodes the media directory path as media.
That might lead to issues when using a CDN or something, because it bypasses Mage::getBaseUrl('media').

5
  • what is the difference between {{store direct_url="media/url"}} and {{media url="foo"}} ?
    – Alex
    Jun 14, 2013 at 15:04
  • Updated my answer accordingly.
    – Vinai
    Jun 14, 2013 at 15:07
  • Thank you. I also edited my question to make it more clear - I want the full URL of a picture. Actually I would be happy to accept my own answer ;-) --- as I know my problem best :-P
    – Alex
    Jun 16, 2013 at 12:07
  • 4
    Note that: {{store direct_url="media/url"}} -> If mod_rewrite is disabled, this may introduce the index.php in the url, hence breaking the media url.
    – MagePsycho
    Jun 16, 2013 at 12:15
  • 1
    @MagePsycho: good point. Lucky I've never had ha store with mod_rewrite disabled.
    – Vinai
    Jun 16, 2013 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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