Categories
General

Linking to a commenter’s author page

Normally, when a logged-in user leaves a comment, a link pointing to the URL in their profile is added to the comment. This is fine, but what if you’ve set up an author template to serve up fancy author pages? Wouldn’t it be better to link there instead? If your theme already has a replacement wp_list_comments function, it’s pretty easy.

Add something like this to the top of your comment callback function in your theme’s functions.php:

$comment_author = get_comment_author();
$comment_author_link = get_comment_author_link();
if (!empty($comment->user_id)){
  if ($author_url = get_author_posts_url($comment->user_id)) {
    $comment_author_link = '<a href="' . $author_url . '">' . $comment_author . '</a>';
  }
}

Now you can echo out $comment_author_linkĀ  wherever you had the standard author link. If the commenter is a logged-in user, the link will point to their fancy author page – otherwise it’ll just point to the commenter’s URL as usual.

Categories
General

How to change things on the comment template

In my case, I wanted to change the text in the new threaded comments from “Reply” to “Reply to this comment” – sounds simple enough, right?

First: Grab the example function from this Codex page and add it to your theme’s functions.php file, changing the function name to suit.

Next: Locate the wp_list_comments function call in your theme’s comments.php file. If you don’t see it there you’ll need to consult the Codex to get your theme up to date. Once you’ve found or added that function call, change it to use your new custom function instead of the default, like this:

wp_list_comments('type=comment&callback=mytheme_comment');

Finally: It’s clear enough how to modify the function to do some things, but in my case I wanted WP’s comment_reply_link function to display “Reply to this comment” instead of just “Reply” – it’s not clear how to do that though. The example function takes some arguments, so I decided to poke around to see if maybe it accepts more. Turns out you can set a custom reply_text by adding to the array of arguments like this:

comment_reply_link(array_merge( $args, array('reply_text' => 'Reply to this comment', 'depth' => $depth, 'max_depth' => $args['max_depth'])))

Other defaults include: add_below, respond_id, login_text, depth before after. Not sure what they all do yet…

Update: The get_avatar call in the example function from the Codex has a placeholder $default set, so it doesn’t work right for users without gravatar accounts. I just removed the placeholder so my get_avatar call looks like this instead:

< ?php echo get_avatar($comment,$size='48'); ?>

Categories
General

Subscribe to Comments plugin hack

Mark Jaquith’s Subscribe to Comments works very well for keeping group blog users aware of comments between visits to the site. However, with comment threading enabled, the links to comments in the emails it sends out are a bit inconvenient – they don’t lead to the comment that’s quoted in the email, which means you must scroll around looking for the comment if you want to reply to it. This hack just adds a link straight to the comment. I submitted a ticket too, so maybe this will be added to the next version.

Insert this at about line 602, just after the code for the regular comment link:

$message .= __("Jump straight to this comment:n",'subscribe-to-comments');
$message .= get_comment_link($comment->comment_ID) . "nn";