1

I would like to change the commenter links as shown in this screenshot:

screenshot

In my test plugin at GitHub I have first tried the following code:

define('PROFILE', '<a href="/user/%s" rel="external nofollow" class="url">%s</a>');

function get_comment_author_link($cid) {
   $uid = get_comment_author($cid);
   return sprintf(PROFILE, $uid->ID, $uid->name);
}

add_action('get_comment_author_link',  'get_comment_author_link');

But then by studying the file wp-includes/comment-template.php I have realized, that the argument to get_comment_author_link is not a comment ID, but a string containing <a href="...">...</a> and thus I can't use this hook for my purpose (because the user ID can't be found in the hook).

Please advise me a suitable hook or way to change the commenter links.

2
+50

You can use the get_comment_author_link hook, which is part of the get_comment_author_link() template tag, like your question already suggests. Then you only have to get the according user_id, which can be done via the $comment global, and construct the link you want accordingly.

Exemplary usage like shown below:

add_filter( 'get_comment_author_link', 'wpse144835_custom_comment_author_link' );
function wpse144835_custom_comment_author_link( $link ) {
    global $comment;
    $comment_user_id = $comment->user_id;
    // code to create the new link
    return $new_link;
}



Edit:

Responding regarding your questions, the according comment is unfortunately deleted, so this is kind of a informational shortlist now. There is a lot in there, way too much to really answer them, but I give you some additional information:

6
  • 1
    Thanks for the references! (Sorry, I've occasionally deleted my old comment - after I've found $comment->comment_author). I will gladly award the bounty in a day. May 22 '14 at 18:33
  • 1
    Ohh, sure, how could I've forgot about that! @AlexanderFarber
    – Nicolai
    May 22 '14 at 18:35
  • Could you please take 1 more look at my site slova.de/archives/29 - both the users "Mr. WordPress" and the user "aaa" (someone registered at my site) have the /user/0. Why do they both have their ID == 0? May 22 '14 at 18:45
  • 1
    0 means that it is a comment by unregistered user; the user mr. wordpress doesn't exist, so it's a unregistered comment, what about your aaa user? @AlexanderFarber
    – Nicolai
    May 22 '14 at 18:56
  • 1
    Not a problem, I'm still learning every day. It depends on your setup under Settings > Discussion. If you want unregistered comments you probably should implement a condition for that, because the profile page only will only work for registered users. @AlexanderFarber
    – Nicolai
    May 22 '14 at 19:13
0

The output of that function, is an a-tag containing a link to the author profile, its input is definitely an ID.

If you'd like to change the output of that function, you need a filter and you'd need to str_replace the a-tag (or similar method acting on the string). (see where to modify get_comment_author_link()?)

I think the more important question is: what exactly are you trying to achieve here?

In theory the commenters link could be implemented differently in every theme/plugin. It could be done with get_comment_author_url() or retrieved straight from the comment object with get_comment(), or any other way really.

However to alter the output of get_comment_author_link(), see the example above.

0

Could you try:

 $id = get_comment( get_comment_ID() )->user_id;

I just found this solution while googling, but I don't have an environment to test it out.

1
  • 1
    This should work, but there are unnecessary steps in it.
    – Nicolai
    May 22 '14 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.