How to make TinyMCE work with Magento CMS pages

Latest version of Magento, as of time of this writing, is 1.3.2.3. Latest, as well as previous versions come with “half-packed” support for TinyMCE editor. There are however few bugs and few steps that need to be handled in order to enable wysiwyg editing in CMS pages. Personally I am not that big of a fan of wysiwyg editors, mostly because I am developer and like to know what I write down.
However, most of the store owners are not developers or even not that much technically savvy people (not that this is a bad thing) and they could use wysiwyg editors when adding, modifying content.
Here I will walk you trough few simple steps that will make you enable wysiwyg editing by using TinyMCE. You can see the screenshots of final result below.
Step 1: Download and unpack TinyMCE to root /js folder. Two things to keep in mind here. Download regular version (not jQuery version) of TinyMCE. This is due to the fact that Magento uses Prototype, so we need to avoid conflicts. Second, watch out for unpack location. Your tiny_mce.js file should be accessible on js/tiny_mce/tiny_mce.js path.
Step 2: Open the app/code/core/Mage/Adminhtml/Block/Cms/Page/Edit/Tab/Main.php file. Locate the
$fieldset->addField('content', 'editor', array(
'name' => 'content',
'label' => Mage::helper('cms')->__('Content'),
'title' => Mage::helper('cms')->__('Content'),
'style' => 'height:36em;',
'wysiwyg' => false,
'required' => true,
));
and change it to
$fieldset->addField('content', 'editor', array(
'name' => 'content',
'label' => Mage::helper('cms')->__('Content'),
'title' => Mage::helper('cms')->__('Content'),
'style' => 'height:36em;',
'wysiwyg' => true,
'theme' => 'advanced',
'required' => true,
));
As you can see, here we changed on existing attribute (‘wysiwyg’) value and added new attribute ‘theme’.
Step 3: Open the /lib/Varien/Data/Form/Element/Editor.php file and locate the method getElementHtml(). Here we change
$html = '
<textarea id="'.$this->getHtmlId().'" class=""textarea" title="'.$this->getTitle().'" name="'.$this->getName().'">getClass().'" '.$this->serialize($this->getHtmlAttributes()).' >'.$this->getEscapedValue().'</textarea>
<script type="text/javascript">
//< ![CDATA[ /* tinyMCE.init({ mode : "exact", theme : "'.$this->getTheme().'",
elements : "' . $element . '",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_path_location : "bottom",
extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
theme_advanced_resize_horizontal : "false",
theme_advanced_resizing : "false",
apply_source_formatting : "true",
convert_urls : "false",
force_br_newlines : "true",
doctype : \'< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\'
});*/
//]]>
</script>';
to
$_storeBaseUrl = str_replace('index.php', '', Mage::getBaseUrl());
//Just to test retrieved url
//echo '$_storeBaseUrl: '.$_storeBaseUrl;
$html = '
<textarea id="'.$this->getHtmlId().'" class=""textarea" title="'.$this->getTitle().'" name="'.$this->getName().'">getClass().'" '.$this->serialize($this->getHtmlAttributes()).' >'.$this->getEscapedValue().'</textarea>
<script language="javascript" type="text/javascript" src="'.$_storeBaseUrl.'/js/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
//< ![CDATA[ Event.observe(window, "load", function() { tinyMCE.init({ mode : "exact", theme : "'.$this->getTheme().'",
elements : "' . $element . '",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_path_location : "bottom",
extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
theme_advanced_resize_horizontal : "false",
theme_advanced_resizing : "false",
apply_source_formatting : "true",
convert_urls : "false",
force_br_newlines : "true",
doctype : \'< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\'
});
});
//]]>
</script>';
As you might noticed, I used $_storeBaseUrl in this other chunk of code to get the valid path to my TinyMCE javascript file. If you are using store that does not have url rewrites this is something that needs to be done because your javascript file would not be found if you go trough link like “http://store.local/index.php/js/tiny_mce/tiny_mce.js”. Then you would get TinyMCE shown on CMS page but it would be disabled.
As you can see, there were only three minor changes needed (download, modify, modify) to get the TinyMCE editor working.
Hope this helps. Cheers.
10 comments
Hello,
some time after, I found that this line is extremely dangerous :
extended_valid_elements : “a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]”,
particularly for the img tag.
In fact id is not on the list for img and this is the source of breaking the widgets code in wysiwyg mode, because widget code/decode needs id to do that correctly.
On the other hand, the extension Pulsestorm_Tinymceconfig, that you can dowload here :
http://alanstorm.com/2013/projects/Pulsestorm_Tinymceconfig-0.1.0.tgz
Is the best clean way to enhanced TinyMCE.
Be careful to put + before the tags if you want to add allowed things and not replace the preceding settings.
Some good things about TinyMCE settings here :
http://www.pixafy.com/blog/2013/01/overcoming-magentos-built-in-wysiwyg-editor/
Hope this will help.
How to static content load on the wysiwyg editor at time of edit action in the controllers?
I wonder how to set TinyMCS to prevent formatting all things. Even when I try to add embed video with responsive code with inline css, tinyMCS after edit change it to pure iframe and I lost whole responsive css from it. Any hints ?
I know that it can TinyMCS cleanup – could be totally disabled – but this is not a good option. Any hints – how to prevent TinyMCS from “improving” embed code with inline css. Thanks for help
I wonder what would be the best way to change tinymce settings in Magento 1.4 – yes i know i can edit core js file, but i guess that this should NOT be the right way
Hey guys,
This will be quite obsolete when Magento 1.4. comes. It will have HTML editor already in the core installation.
Hello,
For 1.3, I had the same problem : buttons didn’t work and no text in TinyMCE. This is because of the tabs behavior in admin panels. They are loaded / unloaded when you go from one to another, and when the page is first loaded (all tabs are unloaded then the selected one is re-loaded).
This make TinyMCE go mad.
See here for more details :
http://blog.mirthlab.com/2008/11/13/dynamically-adding-and-removing-tinymce-instances-to-a-page/
I tried to get this working on a 1.3.2.4 install and the editor appears but none of the buttons work and I cant view any content in the textarea.
Can this be used on the category descriptions as well? If so how?
Magento WYSIWYG extension sometimes messes up when having {{ }}
and removes the last }}. The {{}} are needed for loading Magento variables, Magento store URL ….
As an alternative to setting up TinyMCE manually, you could look at using our Magento WYSIWYG extension, which has been recently updated.