How To Remove Insert Variable & Insert Widget From WYSIWYG Editor In Magento 2
WYSIWYG editor in Magento 2 is an HTML editor that offers “what you see is what you get” functionality. It makes the task of adding, editing and formatting the content easier.
However, sometimes offering this editor gives unnecessary rights and features by allowing them to insert the variable and widget.
For example, the Magento marketplace has multiple users and vendors who can use the WYSIWYG editor from the admin panel. The super admin would not want them to use all the widgets but only allow designing with the editor. In such cases, one can use the below solution.
The default WYSIWYG editor in the Magento 2 is as shown here:
After implementing the below code, the WYSIWYG editor is as shown here:
Method to remove add variable & add widget from WYSIWYG editor in Magento 2:
- Add below code in the system.xml file
12345<field id="description" translate="label comment" type="editor" sortOrder="20" showInDefault="1"showInWebsite="1" showInStore="1"><label>Description </label><frontend_model>Vendor\Extension\Block\Adminhtml\Editor</frontend_model></field> - Create new file Editor.php at Vendor\Extension\Block\Adminhtml folder
12345678910111213141516171819202122232425262728293031<?phpnamespace Vendor\Extension\Block\Adminhtml;use Magento\Backend\Block\Template\Context;use Magento\Cms\Model\Wysiwyg\Config as WysiwygConfig;class Editor extends \Magento\Config\Block\System\Config\Form\Field{public function __construct(Context $context,WysiwygConfig $wysiwygConfig,array $data = []){$this->_wysiwygConfig = $wysiwygConfig;parent::__construct($context, $data);}protected function _getElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element){$element->setWysiwyg(true);$confgiData = $this->wysiwygConfig->getConfig($element);$confgiData->setplugins([]);$confgiData->setadd_variables(0);$confgiData->setadd_widgets(0);$element->setConfig($confgiData);return parent::_getElementHtml($element);}}
That’s it.
Any doubts? Feel free to mention them in the Comments section below. I’ll help you out.
Please share the solution with the Magento community via social media.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How To Get Customer Data From Attribute Value In Magento 2
Hot Fix – Remove Failed Login Attempts From The Database Patch For Magento 2.3.0 – 2.3.2p1 Released
Next