Notifications
Clear all

Putting a text to the end of each comment

11 Posts
3 Users
1 Reactions
1,264 Views
(@mert2312)
Active Member
Joined: 4 years ago
Posts: 6
Topic starter  

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.


   
Quote
Elvina
(@elvina)
Support
Joined: 5 years ago
Posts: 1403
 

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.


   
ReplyQuote
(@mert2312)
Active Member
Joined: 4 years ago
Posts: 6
Topic starter  

@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!


   
ReplyQuote
Elvina
(@elvina)
Support
Joined: 5 years ago
Posts: 1403
 
Posted by: @mert2312

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.


   
mert2312 reacted
ReplyQuote
(@mert2312)
Active Member
Joined: 4 years ago
Posts: 6
Topic starter  

@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?


   
ReplyQuote
Elvina
(@elvina)
Support
Joined: 5 years ago
Posts: 1403
 

@mert2312,

Please leave the example URL to allow us to check it.

 


   
ReplyQuote
(@mert2312)
Active Member
Joined: 4 years ago
Posts: 6
Topic starter  

@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.


   
ReplyQuote
(@mert2312)
Active Member
Joined: 4 years ago
Posts: 6
Topic starter  

@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?


   
ReplyQuote
Astghik
(@astgh)
Illustrious Member Admin
Joined: 7 years ago
Posts: 6101
 

@mert2312,

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);

   
ReplyQuote
(@mert2312)
Active Member
Joined: 4 years ago
Posts: 6
Topic starter  

@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.


   
ReplyQuote
Astghik
(@astgh)
Illustrious Member Admin
Joined: 7 years ago
Posts: 6101
 

@mert2312,

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. 


   
ReplyQuote
Share:
Scroll to top