Removing Automatic
Tags from Static Blocks in Magento
Are you struggling with unwanted automatic <p> tags appearing on your static blocks or pages in Magento, even though you’ve disabled the WYSIWYG editor? You’re not alone! Let’s dive into how to remove these pesky tags without touching the editor.
Understanding the Issue
The default Magento setup automatically wraps any text entered into a static block with a <p> tag. This is done by the core tiny_mce JavaScript library, which is used for the WYSIWYG (What You See Is What You Get) editor. Even if you’ve disabled the editor, the automatic wrapping of text with <p> tags remains.
The Misleading JS File
Many developers initially look into js/mage/adminhtml/wysiwyg/tiny_mce/setup.js to find a solution. However, this file only handles the initialization and configuration of the WYSIWYG editorâit doesn’t control the automatic <p> tag insertion.
The Core Culprit
The real culprit lies in lib/tiny_mce/plugins/wordcount/plugin.js. This file contains the code that automatically wraps text entered into a static block with a <p> tag. To remove these tags, we need to modify this core Magento file.
Important Note: Directly editing core files is generally discouraged as it can cause issues during updates. A better approach would be to create a custom module and override the necessary files. For a detailed guide on creating extensions in Magento 2, check out our official documentation.
Overriding the Core File
To permanently remove the automatic <p> tag wrapping:
-
Create a new module directory under
app/code/Vendor_Name/Module_Name.- Replace
Vendor_Namewith your vendor name (e.g., “LaravelCompany”). - Replace
Module_Namewith a unique identifier for your module, such as “RemovePtag”.
- Replace
-
Inside the
app/code/Vendor_Name/Module_Namedirectory, create anetc/module.xmlfile:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Modularity/etc/module.xsd">
<module name="Vendor_Name_Module_Name" setup_version="1.0.0">
<sequence>
<module name="Magento_TinyMce"/>
</sequence>
</module>
</config>
- This file tells Magento to load your module after the
Magento_TinyMcemodule.
-
Create a new directory
app/code/Vendor_Name/Module_Name/view/adminhtml/templates/tinymce/plugins/wordcount. -
Inside this new directory, create a copy of the original
plugin.jsfile and rename it to something likeplugin_no_p_tag.js. -
Open the original
plugin.jsfile located atlib/tiny_mce/plugins/wordcount/plugin.js. Search for this line:
editor.onKeyUp.add(function (ed, e) {
ed.execCommand('mceInsertContent', false, '<p>' + ed.selection.getContent() + '</p>');
});
- Comment out or remove the entire function to disable the automatic
<p>tag insertion.
- Save your changes and clear Magento’s cache using
php bin/magento cache:cleanin the command line.
Conclusion
Removing the automatic <p> tags from static blocks requires a bit of core file overriding, as it isn’t handled by the WYSIWYG editor configuration alone. By following this guide, you can create a custom module that overrides the core plugin.js file and eliminates those unwanted <p> tags.
Remember, directly editing core files should be avoided if possible. Always consider creating a custom module to maintain your changes during updates.
Happy coding!