Enhancing Magento’s WYSIWYG Editor: Programmatically Adding Custom Variables
As an experienced Magento developer, I often help clients customize their online stores to meet unique business requirements. One common request is the need to add custom variables to the “Insert Variable” popup of Magento’s WYSIWYG editor.
Adding custom variables to Magento’s TinyMCE editor involves a combination of configuration changes and custom code development. In this post, I’ll guide you through the process step-by-step.
Understanding Magento’s Default Variables
Before we dive into adding custom variables, let’s first understand what is available by default in your Magento installation.
Magento ships with several predefined variables that you can insert using the “Insert Variable” icon in the editor toolbar. These include:
- Product attributes
- Store information (name, code)
- Customer information
- Date and time
To view a full list of these built-in variables, navigate to Admin > Stores > Settings > Configuration > General > Design. Scroll down to the heading “HTML Head” section.
Defining Your Custom Variables
Now let’s say you want to add custom variables for your product attributes. For example, you might need to display the product’s SKU or manufacturer in a specific part of your content.
The first step is to define these new variable options. To do this, open your editor and navigate to Admin > Stores > Settings > Configuration > General > Design.
Under “HTML Head”, look for the field titled “Custom Variables (Comma Separated Values)”. This field allows you to specify additional variables that can be inserted using the WYSIWYG editor.
Enter your custom variable names here, separated by commas. For instance: my_product_sku, my_product_manufacturer.
Creating a Custom Module
To make our changes persistent and avoid hardcoding values directly into Magento’s core files, we’ll create a custom module. This ensures that our changes remain intact even when you update your Magento version.
First, navigate to the root directory of your Magento installation (usually /var/www/html/magento2). Create a new folder named My_CustomVariables.
Inside this folder, create another folder called etc. Inside etc, add a di.xml file. This XML file will define our custom configuration.
The contents of di.xml should look like this:
<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\ConfigInterface">
<arguments>
<argument name="customVariables" xsi:type="array">
<item name="my_product_sku" xsi:type="string">Product SKU</item>
<item name="my_product_manufacturer" xsi:type="string">Manufacturer</item>
</argument>
</arguments>
</type>
</config>
In this di.xml file, we’re defining the custom variables we entered earlier in the configuration settings. The names of these variables are mapped to human-readable descriptions.
Including Custom Variables in Your Templates
To use your new variables in your templates (like CMS pages or product descriptions), you can directly insert them into the HTML.
Here’s an example:
<div class="my-product-info">
<p>Product SKU: {my_product_sku}</p>
<p>Manufacturer: {my_product_manufacturer}</p>
</div>
Remember to replace {my_product_sku} and {my_product_manufacturer} with the actual variable names you defined in your di.xml file.
Conclusion
Adding custom variables to Magento’s WYSIWYG editor is a powerful way to extend the functionality of your store. By following this guide, you’ve learned how to create custom variables programmatically and include them in your templates without modifying core files directly.
This approach ensures that your changes remain intact even when you update your Magento installation or apply patches.
If you found this tutorial helpful, I encourage you to explore more advanced Magento customizations on Laravel Company’s Blog. We regularly publish articles on best practices, performance optimizations, and innovative use cases for the Magento platform.
Happy coding!