10 Handy WordPress Comments Hacks
Comments sections are neglected on many blogs. That is definitely a bad thing, because comments represent interaction between you and your readers. In this article, we’ll have a look at 10 great tips and hacks to enhance your blog’s comments section and give it the quality it deserves.
1. Add Action Links To Comments
The problem.
Whether or not you allow readers to add comments without having to be approved, you will often need to edit, delete or mark certain comments as spam. By default, WordPress shows the “Edit” link on comments (using the edit_comment_link()
function) but not “Delete” or “Spam” links. Let’s add them.
The solution.
First, create a function. Paste the code below in your functions.php file:
function delete_comment_link($id) { if (current_user_can('edit_post')) { echo '| <a href="'.admin_url("comment.php?action=cdc&c=$id").'">del</a> '; echo '| <a href="'.admin_url("comment.php?action=cdc&dt=spam&c=$id").'">spam</a>'; } }
Once you have saved functions.php, open up your comments.php file, and add the following code where you want the “Delete” and “Spam” links to appear. They must go in the comment loop. In most themes, you’ll find an edit_comment_link()
declaration. Add the code in just after that.
delete_comment_link(get_comment_ID());
Code explanation.
The first thing we did, of course, was to make sure the current user has permission to edit comments. If so, links to delete and mark a comment as spam are displayed. Note the use of the admin_url()
function, which allows you to retrieve your blog admin’s URL.
Source:
2. Separate TrackBacks From Comments
The problem.
Do your posts have a lot of TrackBacks? Mine do. Trackbacks are cool because they allow your readers to see which articles from other blogs relate to yours. But the more TrackBacks you have, the harder the discussion is to follow. Separating comments from TrackBacks, then, is definitely something to consider, especially if you do not use the “Reply” capabilities introduced in WordPress 2.7.
The solution.
Open and edit the comments.php file in your theme. Find the comment loop, which looks like the following:
foreach ($comments as $comment) : ?> // Comments are displayed here endforeach;
Once you have that, replace it with the code below:
<ul class="commentlist"> <?php //Displays comments only foreach ($comments as $comment) : ?> <?php $comment_type = get_comment_type(); ?> <?php if($comment_type == 'comment') { ?> <li>//Comment code goes here</li> <?php } endforeach; </ul> <ul> <?php //Displays trackbacks only foreach ($comments as $comment) : ?> <?php $comment_type = get_comment_type(); ?> <?php if($comment_type != 'comment') { ?> <li><?php comment_author_link() ?></li> <?php } endforeach; </ul>
Code explanation.
Nothing hard about this code. The get_comment_type()
function tells you if something is a regular comment or a TrackBack. We simply have to create two HTML lists, filling the first with regular comments and the second with TrackBacks.
Source:
3. Get Rid Of HTML Links In Comments
The problem.
Bloggers are always looking to promote their blogs, and spammers are everywhere. One thing that totally annoys me on my blogs is the incredible amount of links left in comments, which are usually irrelevant. By default, WordPress transforms URLs in comments to links. Thankfully, if you’re as tired of comment links as I am, this can be overwritten.
The solution.
Simply open your function.php file and paste in this code:
function plc_comment_post( $incoming_comment ) { $incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']); $incoming_comment['comment_content'] = str_replace( "'", ''', $incoming_comment['comment_content'] ); return( $incoming_comment ); } function plc_comment_display( $comment_to_display ) { $comment_to_display = str_replace( ''', "'", $comment_to_display ); return $comment_to_display; } add_filter('preprocess_comment', 'plc_comment_post', '', 1); add_filter('comment_text', 'plc_comment_display', '', 1); add_filter('comment_text_rss', 'plc_comment_display', '', 1); add_filter('comment_excerpt', 'plc_comment_display', '', 1);
Once you have saved the file, say goodbye to links and other undesirable HTML in your comments.
Code explanation.
The first thing we did was create two functions that replace HTML characters with HTML entities. Then, using the powerful add_filter()
WordPress function, we hooked the standard WordPress comments processing functions to the two functions we just created. This makes sure that any comments added will have their HTML filtered out.
Sources: