Loading..
Processing... Please wait...

Product was successfully added to your shopping cart.



[Bug Fixed] Magento 2 category description wysiwyg editor uploads broken image on frontend

Here is a Magento 2 wysiwig editor bug which is still present in the latest CE 2.1.5 (as of time of writing).

 

The problem description

Disclaimer: In the latest Magento CE 2.1.5 it seems to only affect category descriptions while in earlier versions it might affects product descriptions as well.

If you attempt to upload and add an image to any category description via wysiwyg editor:

Magento 2 Category Description wysiwyg editor Magento 2 Category Description wysiwyg editor

It is displayed on frontend as containing your admin url which is not only a security risk (revealing your backend path) but is also leading to a broken image if you are not logged in to admin:

Magento 2 broken image url Magento 2 broken image url

I discovered this bug while answering Magento Stackexchange question which leads me an open github issue.

 

The fix

 

Disclaimer: Once you implement the following fix you still need re-upload all saved category description images (if any).

 

The suggested fix would be to write an interceptor and add 'add_directives' => true to wysiwyg getConfig function.

Your etc/di.xml file:

<?xml version="1.0"?>
<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>

 

Your Vendor/Module/Plugin/WysiwygConfig.php file:

<?php

namespace Vendor\Module\Plugin;

class WysiwygConfig
{
    public function afterGetConfig($subject, \Magento\Framework\DataObject $config)
    {
        $config->addData([
            'add_directives' => true,
        ]);

        return $config;
    }
}

 

Install it php bin/magento setup:upgrade and you are all set.

Here it is all packed up (not by me) as an extension.

 

 

 

 

If you find this post interesting do not hesitate to sign up for our newsletter and join the 692 people who receive Magento news, tips and tricks regularly.

Thank You!