3

There is an author_link hook which is very useful for posts and bios; however, I don't see a hook for comment author link. Does one exist?

3 Answers 3

3

The other two answers probably answer your question, but here's a way look at WordPress source code to be able to determine for yourself whether a certain hook exists for your purposes.

You ask whether there is a hook for the comment author link. So let's look at the source for that function: comment_author_link().

We see that there's not really much to that function. It just echos the output of get_comment_author_link(). So let's look at that function.

It does some stuff and then returns the filtered results of get_comment_author_link. So you could use that filter to modify the output of comment_author_link().

add_filter( 'get_comment_author_link', function( $return, $author, $comment_id ){
  if( 'example' === $author ) {
    $return = '<a href="https://example.com/">Example</a>';
  }
  return $return;
}, 10, 3 );

But I have a feeling that you want to modify the URL of the comment author. If that's the case, we see that get_comment_author_link() calls get_comment_author_url() to determine the author.

And in that function we see that it returns the filtered results of get_comment_author_url.

So if you want to change the URL of the comment author link, you could do something like:

add_filter( 'get_comment_author_url', function( $url, $id, $comment ){
  if( 'example' === $comment->comment_author ) {
    $url = 'https://example.com/';
  }
  return $url;
}, 10, 3 );
1
  • 1
    Upvoted, but I'd avoid using anonymous callbacks because removing them can be complicated. Apr 3, 2018 at 20:10
2

There is a filter hook like author_link hook.

get_comment_author_link()

Details info about the above filter hook.
https://developer.wordpress.org/reference/hooks/get_comment_author_link/

There are a lot of built-in functions at wordpress.

<?php comment_author_url_link('linktext', 'before', 'after'); ?> 

Let's have a look the functions related to comments.
https://codex.wordpress.org/Function_Reference/comment_author_url_link

I hope you will find the solution.

0

Are you looking for get_comment_author_url ?

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.