Magento – Change URL of Uploaded WYSIWYG Image Paths

While working on a Magento CE 1.7 site, the client was tired of seeing crazy URLs for his uploaded images (directives!) for everything that he was uploading through his WYSIWYG editor for products, CMS pages, etc. I wanted to change this to something simple – basically, just the wysiwyg URL, which is a readable link for most everyone.

We will be modifying a core file (overwriting it with a local file obviously!)

File to edit: /app/code/core/Mage/Cms/Helper/Wysiwyg/Images.php (Around Line 172, getImageHtmlDeclaration() function)

public function getImageHtmlDeclaration($filename, $renderAsTag = false)
    {
        $fileurl = $this->getCurrentUrl() . $filename;
        $mediaPath = str_replace(Mage::getBaseUrl('media'), '', $fileurl);
        $directive = sprintf('{{media url="%s"}}', $mediaPath);
        if ($renderAsTag) {
            $html = sprintf('<img src="%s" alt="" />', $this->isUsingStaticUrlsAllowed() ? $fileurl : $directive);
        } else {
            if ($this->isUsingStaticUrlsAllowed()) {
                $html = $fileurl; // $mediaPath;
            } else {
                $directive = Mage::helper('core')->urlEncode($directive);
                $html = Mage::helper('adminhtml')->getUrl('*/cms_wysiwyg/directive', array('___directive' => $directive));
            }
        }
        return $html;
    }

We will change the $html variable in the 2nd else statement. Create your new file:
File: /app/code/local/Mage/Cms/Helper/Wysiwyg/Images.php

public function getImageHtmlDeclaration($filename, $renderAsTag = false)
    {
        $fileurl = $this->getCurrentUrl() . $filename;
        $mediaPath = str_replace(Mage::getBaseUrl('media'), '', $fileurl);
        $directive = sprintf('{{media url="%s"}}', $mediaPath);
        if ($renderAsTag) {
            $html = sprintf('<img src="%s" alt="" />', $this->isUsingStaticUrlsAllowed() ? $fileurl : $directive);
        } else {
            if ($this->isUsingStaticUrlsAllowed()) {
                $html = $fileurl; // $mediaPath;
            } else {
                $directive = Mage::helper('core')->urlEncode($directive);
                //$html = Mage::helper('adminhtml')->getUrl('*/cms_wysiwyg/directive', array('___directive' => $directive));
				$html = $fileurl;
            }
        }
        return $html;
    }

Download new file here: Images.php

Posted in Magento, Magento Enterprise, PHP, WYSIWYG
  • Renato Doe

    Thanks heaps mate. It fixed my problem in Magento 2.

  • Ciro Attanasio

    Wery Well ,Thanks