Magento – Magento 2 WYSIWYG Media image directive using admin url

magento-2.1mediaurlwysiwyg

Why is magento 2 creating directives for media images using the admin url?

for example when i add an image on category page WYSIWYG it adds

<img src="{{media url="wysiwyg/image.jpg"}}" alt="" />

but then magento parses it for frontend and is like this

<img src="https://domain.co.uk/admin/cms/wysiwyg/directive/___directive/e3ttZWRpYSB1cmw9Ind5c2l3eWcvQ29udmV5b3JfYmVsdHNfZmFzdF9kZWxpdmVyeS5qcGcifX0,/key/b67d0a8069ef28a8443e0bad6d912512704213d60e1d9021b1ec2b9dd34bf390/" alt="">

because its linking to admin the only way it will load on browser is if you are logged in to the admin. This also poses a security issue because it is disclosing the admin path on frontend.

I looked in vendor/magento/module-cms/Helper//Wysiwyg/images.php and looks like the function getImageHtmlDeclaration() generates this

   public function getImageHtmlDeclaration($filename, $renderAsTag = false)
    {
        $fileurl = $this->getCurrentUrl() . $filename;
        $mediaUrl = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
        $mediaPath = str_replace($mediaUrl, '', $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 = $this->urlEncoder->encode($directive);
                $html = $this->_backendData->getUrl('cms/wysiwyg/directive', ['___directive' => $directive]);
            }
        }
        return $html;
    }

I tried to use static urls for media but still no use so the only work around I can think of is to edit this function to use frontend url instead of backend/admin

any help on this would be very much appreciated 🙂

Best Answer

This is a known bug which is still present in CE 2.1.5.

The known fix is to add 'add_directives' => true to getConfig function ofvendor/magento/module-cms/Model/Wysiwyg/Config.php.

The best way to do that is to write an interceptor.

  1. In your custom Magento 2 extension's etc/di.xml file:

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
      <type name="Magento\Cms\Model\Wysiwyg\Config">
       <plugin name="add_wysiwyg_data" type="Vendor\Module\Plugin\WysiwygConfig" sortOrder="30" />
      </type>
    </config>
    
  2. Vendor\Module\Plugin\WysiwygConfig.php:

    namespace Vendor\Module\Plugin;
    
    class WysiwygConfig
    {
     public function afterGetConfig($subject, \Magento\Framework\DataObject $config)
     {
       $config->addData([
        'add_directives' => true,
       ]);
    
       return $config;
     }
    }
    
  3. Install it php bin/magento setup:upgrade

  4. Important: after installation you need to re-edit the affected category descriptions and re-upload images.

Idea of this fix extension is not mine but this guy. He also packed it all up on github for you to download.

I tested it myself on CE 2.1.4 and it works fine.

Related Topic