Method of Adding Responses to WordPress Theme Comments

Time:2019-8-10

This article gives an example of how to add replies to WordPress topic reviews. Share for your reference. Specifically as follows:

Many friends want to add an @reply reply effect to their thematic comments, they will choose to use plug-ins, in fact, we can completely modify the source code to achieve, here to introduce you how to add @reply reply in WordPress thematic comments.

The methods are as follows:

First, add the following JS code to comments.php:

Copy code

The code is as follows:

<script language=”javascript”>
//<![CDATA[
function to_reply(commentID,author) {
var nNd=’@’+author+’:’;
var myField;
if (document.getElementById(‘comment’) && document.getElementById(‘comment’).type == ‘textarea’) {
myField = document.getElementById(‘comment’);
} else {
return false;
}
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = nNd;
myField.focus();
}
else if (myField.selectionStart || myField.selectionStart == ‘0’) {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
var cursorPos = endPos;
myField.value = myField.value.substring(0, startPos)
+ nNd
+ myField.value.substring(endPos, myField.value.length);
cursorPos += nNd.length;
myField.focus();
myField.selectionStart = cursorPos;
myField.selectionEnd = cursorPos;
}
else {
myField.value += nNd;
myField.focus();
}
}
//]]>
</script>

2. Add the following code to functions. PHP

Copy code

The code is as follows:

// Reply reply function
function to_reply() {
?>
<a onclick=’to_reply(“<?php comment_ID() ?>”, “<?php comment_author();?>”)’ href=”#respond” style=”cursor:pointer;”/>[@reply]</a>
<?php
}
?>

III. On the Commentary Page

Copy code

The code is as follows:

<?php comment_author_link() ?>

Add the “Reply Button” after it. The code is as follows:

Copy code

The code is as follows:

<strong><?php to_reply(); ?></strong>

I hope this article will be helpful to the construction of WordPress.