How to Enable Comments Quote in WordPress Theme

WordPress allows replying to a comments. But sometimes, we may need to quote someones idea to reply to another one’s comments. At that time, we can enable comments quote function into WordPress theme. Just use the following snippets to build your custom comments template:

Put this JS code to your WordPress theme as a .js file:

(function() {
 
function $(id) {
	return document.getElementById(id);
}
window['RE'] = {};
window['RE']['$'] = $;
function quote(authorId, commentId, commentBodyId, commentBox) {
	var author = RE.$(authorId).innerHTML;
	var comment = RE.$(commentBodyId).innerHTML;
 
	var insertStr = '&lt; blockquote &gt;'; insertStr += '\n&lt; strong&gt;<a href="#' + commentId + '">' + author.replace(/\t|\n|\r\n/g, "") + '</a>:'; insertStr += comment.replace(/\t/g, ""); insertStr += '&lt; /blockquote&gt;\n';
insertQuote(insertStr, commentBox);
}
function insertQuote(insertStr, commentBox) { if(RE.$(commentBox) &amp;&amp; RE.$(commentBox).type == 'textarea') {
field = RE.$(commentBox);
 } else { alert("The comment box does not exist!");
return false;
} if(document.selection) {
field.focus();
sel = document.selection.createRange();
sel.text = insertStr;
field.focus();
} else if (field.selectionStart || field.selectionStart == '0') {
var startPos = field.selectionStart;
var endPos = field.selectionEnd;
var cursorPos = startPos;
field.value = field.value.substring(0, startPos) + insertStr + field.value.substring(endPos, field.value.length);
cursorPos += insertStr.length;
field.focus();
field.selectionStart = cursorPos;
field.selectionEnd = cursorPos;
} else {
field.value += insertStr; field.focus();
}
}
window['RE_CON'] = {};
window['RE_CON']['quote'] = quote; }
)
();

Then enable custom comment template in functions.php file and put a “quote” link in comments template.

<a onclick="RE_CON.quote('commentauthor-&lt;?php comment_ID() ?&gt;', '&lt; ?php echo $add_below-$comment-&gt;comment_ID ?&gt;', 'body-&lt;?php comment_ID() ?&gt;', 'comment');" href="javascript:void(0);"> Quote </a>

commentauthor- and body- should be customed according to your template.

Leave a Reply

Your email address will not be published. Required fields are marked *