Hi. I'm looking for a action hook which helps me to put the length of comment in a text, for each comment posted. I also need to get access to the user id of the comment author. For example, after each comment posted, I want to put a text saying: The user id of the comment author is : x and the length of the comment is : y.
Any help would be appreciated.
Hi @mert2312,
You should use the following hook to display the comment author name and the comment length message:
add_filter("comment_text", function ($content, $comment) {
$content .= "comment author is : " . $comment->user_id . " and the length of the comment is : " . strlen($comment->comment_content) . ".";
return $content;
}, 50, 2);
The hook should be added in the current active theme functions.php file.
@elvina Real thanks for such a comprehensive and quick reply. There is only one more question: is that going to change the comment content on the database, or just what is shown on the screen? Thank you so much!
is that going to change the comment content on the database, or just what is shown on the screen?
no, the comment content doesn't change in the database. It only displays the comment author name and the comment length message in the frontend.
@elvina hello again, I was able to implement the code easily, thank your for that. But I had a problem afterwards: the button for "load more comments" is no longer working, it gets stuck. I think it uses AJAX but a problem occurs when I activate this code. How can I make both of them work?
@elvina It's on multiple pages, but here's one of them: https://www.glonera.com/urun/diadermine-hydralist-nemlendirici-bakim-kremi-ipeksi-doku-50-ml/
Thank you.
@elvina hello again. I had to remove the code since it interferes on the comments section on the admin panel and causing some fatal errors afterwards. how can I make sure that the code is only shown on wpdiscuz comments section? Is there a hook specifically for this purpose?
Please use the code below:
add_filter("comment_text", function ($content, $comment) {
if (!is_admin() || (is_admin() && wp_doing_ajax())) {
$content .= "comment author is : " . $comment->user_id . " and the length of the comment is : " . strlen($comment->comment_content) . ".";
}
return $content;
}, 50, 2);
@astghik hello, thank for your reply. that worked for admin part and resolved the error issues, but ajax part is still not working. maybe the function wp_doing_ajax() should be replaced with something else or modified somehow, but I don't know how to deal with this. still a huge help, thanks for that.
We see everything is working fine now. Please explain using some screenshots the issue you see. I'd also suggest you press CTRL + F5 (twice) on the frontend before checking.