<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Broken Bytes &#187; fix</title>
	<atom:link href="http://blog.brokenbytes.info/tag/fix/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.brokenbytes.info</link>
	<description></description>
	<lastBuildDate>Tue, 17 May 2011 20:51:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>WordPress &#8211; MCEComments and Threaded Comments Fix</title>
		<link>http://blog.brokenbytes.info/2010/02/wordpress-tinymcecomments-and-threaded-comments-fix/</link>
		<comments>http://blog.brokenbytes.info/2010/02/wordpress-tinymcecomments-and-threaded-comments-fix/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 02:21:10 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[fix]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[MCEComments]]></category>
		<category><![CDATA[problem]]></category>
		<category><![CDATA[problems]]></category>
		<category><![CDATA[threaded comments]]></category>
		<category><![CDATA[TinyMCE]]></category>
		<category><![CDATA[tinymcecomments]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://blog.brokenbytes.info/?p=123</guid>
		<description><![CDATA[Edit: After further investigation it looks like the only real problem was problem number 2 (hidden field and iframe named the same). All you have to do is implement the fix for problem 2 discussed below and it should work fine. Problem number 2 was more of a conflict and wasn&#8217;t really anyones fault, but [...]]]></description>
			<content:encoded><![CDATA[<hr />
Edit: After further investigation it looks like the only real problem was problem number 2 (hidden field and iframe named the same). All you have to do is implement the fix for problem 2 discussed below and it should work fine.<br />
Problem number 2 was more of a conflict and wasn&#8217;t really anyones fault, but was rather troublesome to fix. We thought it would be easier to change the TinyMCEComments plugin then modify the install of WordPress. We also had already modified the plugin to remove/insert TinyMCE boxes correctly so we might as well keep working with that. To fix the problem before TinyMCE is initialized we change the hidden field with id=&#8221;comment_parent&#8221; so it is id=&#8221;comment_parent_hf&#8221;. This doesn&#8217;t effect WordPress comment submission because form submissions use the name field server side to identify an attribute.</p>
<p>Change in tinyMCEComments.php</p>
<p>Before (start line 295):</p>
<pre class="brush: jscript; title: ; notranslate">
if (subBtn != null) {
	subBtn.onclick=function() {
		var inst = tinyMCE.getInstanceById(&quot;comment&quot;);
		document.getElementById(&quot;comment&quot;).value = inst.getContent();
		document.getElementById(&quot;commentform&quot;).submit();
		return false;
	}
}
tinyMCEPreInit.go();
tinyMCE.init(tinyMCEPreInit.mceInit);
</pre>
<p>After:</p>
<pre class="brush: jscript; title: ; notranslate">
var subBtn = document.getElementById(&quot;submit&quot;);
	if (subBtn != null) {
	subBtn.onclick=function() {
		var inst = tinyMCE.getInstanceById(&quot;comment&quot;);
		document.getElementById(&quot;comment&quot;).value = inst.getContent();
		document.getElementById(&quot;commentform&quot;).submit();
		return false;
	}
}
tinyMCEPreInit.go();
tinymce.dom.Event.add(window, &quot;load&quot;, function() {
	// Change the hidden fields id
	var pId = document.getElementById(&quot;comment_parent&quot;);
	if(pId) pId.id = &quot;comment_parent_hf&quot;;
	tinyMCE.init(tinyMCEPreInit.mceInit);
});
</pre>
<p>The last thing that was changed in the script was a hard coded name of the comment_parent hidden filed inside comment-reply.dev.js in the TinyMCEComments plugin folder. Change parent = t.I(&#8216;comment_parent&#8217;) to parent = t.I(&#8216;comment_parent_hf&#8217;) to reflect the change we make on the load of TinyMCEComments:</p>
<p>Before (line 3):</p>
<pre class="brush: jscript; title: ; notranslate">
var t = this, div, comm = t.I(commId), respond = t.I(respondId), cancel = t.I('cancel-comment-reply-link'), parent = t.I('comment_parent'), post = t.I('comment_post_ID');
</pre>
<p>After:</p>
<pre class="brush: jscript; title: ; notranslate">
var t = this, div, comm = t.I(commId), respond = t.I(respondId), cancel = t.I('cancel-comment-reply-link'), parent = t.I('comment_parent_hf'), post = t.I('comment_post_ID');
</pre>
<p>Now it works! Woohoo! I&#8217;m going to try to contact the other and have these changes included in the next release so everyone can enjoy threaded comments with TinyMCEComments. Thanks for reading and hopefully someone will find this helpful.</p>
<hr />
<p>Today at work I was trying to convert our blog theme to a wider stance for an upcoming template switch, I thought this would be a very simple task. Little did I know the long day that would ensue. Our blog system at work used to use a plugin to create threaded functionality for posts. My boss decided that since WordPress now supports threaded comments we would switch over to that. I thought this would be a great idea (the less code, the less complex, the better). I began to convert our comments template to the new version required for <a href="http://codex.wordpress.org/Migrating_Plugins_and_Themes_to_2.7/Enhanced_Comment_Display" target="_blank">Threaded Comments</a>. After this was done I tried it out. It wouldn&#8217;t work with <a href="http://wordpress.org/extend/plugins/tinymcecomments/" target="_blank">TinyMCEComments</a> (plugin) enabled, but worked fine with it off. When <a href="http://wordpress.org/extend/plugins/tinymcecomments/" target="_blank">TinyMCEComments</a> was enabled it created two TinyMCE textareas whenever you tried to do a threaded reply. This seemed very odd to me but I had a feeling it had to do with moving the comment box below the comment that you&#8217;re replying too. After quite awhile and a little bit of help from my boss I was able to figure out all the issues and get <a href="http://wordpress.org/extend/plugins/tinymcecomments/" target="_blank">TinyMCEComments</a> working with the new threaded comments feature. There were two main issues:</p>
<ol>
<li>Double insertion of TinyMCE editors.</li>
<li>TinyMCE has a bug where if create an editor with an id, then destroy that editor, and then try to create a new editor with that same id it will not work.</li>
<li>TinyMCE names its iframe the id of the textarea concatenated with &#8220;_parent&#8221;. So when you add TinyMCE functionality to a textare with id=&#8221;comments&#8221; you get an iframe with id=&#8221;comments_parent&#8221;. Unfortunately, WordPress uses &#8220;comment_parent&#8221; as the id for a hidden field used to transfer information about where your new comment should be added in the threading (or to the base comments). The hidden fields are output by the function comment_id_fields(). Also WordPress says <a href="http://codex.wordpress.org/Migrating_Plugins_and_Themes_to_2.7/Enhanced_Comment_Display" target="_blank">&#8220;the comment textarea, it MUST have an id=&#8221;comment&#8221;. The JavaScript expects it for focus purposes. If you used anything else, change it. Note that because of this, no other element on your page can have the &#8220;comment&#8221; ID&#8221;</a>.</li>
</ol>
<p>Before I explain the fixes let me first say that <a href="http://wordpress.org/extend/plugins/tinymcecomments/" target="_blank">TinyMCEComments</a> overrides the addComment.moveForm function that is used by WordPress to move the comment box under the comment you want to reply too. It has the same code except that it has the added functionality for enabling TinyMCE on the comment box. Unfortunately it doesn&#8217;t work out of the box with WordPress&#8217; thread comments.</p>
<p>Problem 1 was an issue with the TinyMCEComments plugin and TinyMCE. Using the remove method doesn&#8217;t remove the the TinyMCE&#8217;s iframe from the DOM. This was fixed with Problem 2. The removing and adding method being used doesn&#8217;t work correctly for Problem number 2 is a TinyMCE problem and we can&#8217;t really do to much about it other then implementing a name change when we reinsert the comment box. So we modified the TinyMCEComments plugin to rename the text area on reinsertion.</p>
<p>Before</p>
<p>Remove TinyMCE Comment box:</p>
<pre class="brush: jscript; title: ; notranslate">
try {
   tinyMCE.triggerSave();
   tinyMCE.execCommand('mceFocus', false,'comment');
   tinyMCE.execCommand('mceRemoveControl', false,'comment');
} catch(el) { console.error(el); }
</pre>
<p>Add TinyMCE Comment box code:</p>
<pre class="brush: jscript; title: ; notranslate">
addTiny : function() {
   try { tinyMCE.execCommand('mceAddControl', false, 'comment');
   } catch (e) { console.error(e); }
}
</pre>
<p>After:</p>
<p>Remove TinyMCE Comment box:</p>
<pre class="brush: jscript; title: ; notranslate">
// **** Remove the TinyMCE Editor
var editor = tinymce.EditorManager.activeEditor;
var editorId = editor.getParam('elements');
editor.remove();
tinymce.DOM.remove(editor.getContainer());
editor.destroy();
tinymce.EditorManager.activeEditor = null;
</pre>
<p>Add TinyMCE Comment box code:</p>
<pre class="brush: jscript; title: ; notranslate">
var commentElement = document.getElementById(editorId);
if (editorId == &quot;comment&quot;)
	editorId = &quot;comment_1&quot;;
else {
	var count = parseInt(editorId.replace(/comment_/, &quot;&quot;), 10) + 1;
	editorId = &quot;comment_&quot; + count;
}
commentElement.id = editorId;
tinyMCEPreInit.mceInit.elements = editorId;
tinyMCE.init(tinyMCEPreInit.mceInit);
</pre>
<p>Problem number 2 was more of a conflict and wasn&#8217;t really anyones fault, but was rather troublesome to fix. We thought it would be easier to change the TinyMCEComments plugin then modify the install of WordPress. We also had already modified the plugin to remove/insert TinyMCE boxes correctly so we might as well keep working with that. To fix the problem before TinyMCE is initialized we change the hidden field with id=&#8221;comment_parent&#8221; so it is id=&#8221;comment_parent_hf&#8221;. This doesn&#8217;t effect WordPress comment submission because form submissions use the name field server side to identify an attribute.</p>
<p>Change in tinyMCEComments.php</p>
<p>Before (start line 295):</p>
<pre class="brush: jscript; title: ; notranslate">
if (subBtn != null) {
	subBtn.onclick=function() {
		var inst = tinyMCE.getInstanceById(&quot;comment&quot;);
		document.getElementById(&quot;comment&quot;).value = inst.getContent();
		document.getElementById(&quot;commentform&quot;).submit();
		return false;
	}
}
tinyMCEPreInit.go();
tinyMCE.init(tinyMCEPreInit.mceInit);
</pre>
<p>After:</p>
<pre class="brush: jscript; title: ; notranslate">
var subBtn = document.getElementById(&quot;submit&quot;);
	if (subBtn != null) {
	subBtn.onclick=function() {
		var inst = tinyMCE.getInstanceById(&quot;comment&quot;);
		document.getElementById(&quot;comment&quot;).value = inst.getContent();
		document.getElementById(&quot;commentform&quot;).submit();
		return false;
	}
}
tinyMCEPreInit.go();
tinymce.dom.Event.add(window, &quot;load&quot;, function() {
	// Change the hidden fields id
	var pId = document.getElementById(&quot;comment_parent&quot;);
	if(pId) pId.id = &quot;comment_parent_hf&quot;;
	tinyMCE.init(tinyMCEPreInit.mceInit);
});
</pre>
<p>The last thing that was changed in the script was a hard coded name of the comment_parent hidden filed inside comment-reply.dev.js in the TinyMCEComments plugin folder. Change parent = t.I(&#8216;comment_parent&#8217;) to parent = t.I(&#8216;comment_parent_hf&#8217;) to reflect the change we make on the load of TinyMCEComments:</p>
<p>Before (line 3):</p>
<pre class="brush: jscript; title: ; notranslate">
var t = this, div, comm = t.I(commId), respond = t.I(respondId), cancel = t.I('cancel-comment-reply-link'), parent = t.I('comment_parent'), post = t.I('comment_post_ID');
</pre>
<p>After:</p>
<pre class="brush: jscript; title: ; notranslate">
var t = this, div, comm = t.I(commId), respond = t.I(respondId), cancel = t.I('cancel-comment-reply-link'), parent = t.I('comment_parent_hf'), post = t.I('comment_post_ID');
</pre>
<p>Now it works! Woohoo! I&#8217;m going to try to contact the other and have these changes included in the next release so everyone can enjoy threaded comments with TinyMCEComments. Thanks for reading and hopefully someone will find this helpful.</p>
<p>Completed Files in Zip: <a title="Zipped Modified TinyMCEComments" href="http://blog.brokenbytes.info/wp-content/uploads/2010/02/devtinymcecomments.zip">DevTinyMCEComments</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.brokenbytes.info/2010/02/wordpress-tinymcecomments-and-threaded-comments-fix/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

