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 WordPress Theme.
First, we need to custom comment template in functions.php file.
Then put this JS code to your wordpress theme:
View Code PHP
(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 = '< blockquote >'; insertStr += '\n< strong><a href="#' + commentId + '">' + author.replace(/\t|\n|\r\n/g, "") + '</a>:'; insertStr += comment.replace(/\t/g, ""); insertStr += '< /blockquote>\n'; insertQuote(insertStr, commentBox); } function insertQuote(insertStr, commentBox) { if(RE.$(commentBox) && 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.
View Code PHP
<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.
