How to Build a Quote Function in WordPress Theme

Some times we need to quote others comments in our comment. So theme builder should consider to build this function in comments template. Here I want to share one solution to Build a Quote Function in Theme.

First, we need to custom comment template in functions. file.

Then put this JS code to your wordpress theme:

(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 put a quote link in comments template.

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

commentauthor- and body- should be custom according to your template. You can even find some discount coupon codes to purchase software.

留下评论







注意: 第一次留言的用户,您的评论需要审核通过才会显示,抱歉!