Support » Fixing WordPress » Author Link in a Comment

  • asechrest

    (@asechrest)


    Howdy. Havin’ a bit of trouble with this one.

    I use a custom author.php. Currenty I use the_author_posts_link() to create a link to the author’s page in index and single post views.

    I’m looking for a similar function to link to a comment author’s page, but I haven’t found a similar function. It appears that comment_author_link() creates a link to the author’s personal URL if they provided one, rather than to an author.php.

    Does a function like this exist for comment authors?

    Thanks. 🙂

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter asechrest

    (@asechrest)

    If anyone comes across this thread, I came up with a solution – Linking a Comment Author to an Author Page.

    WordPress provides a function to generate a comment author link, but unlike the function that generates a post author link, the comment author link automatically links to whatever the user has put as their external URL.

    This code allows you to link to the author page. Suggestions on how to improve the code are welcomed. Thanks.

    Oooh, I really don’t like solutions that make people change template files. A cleaner approach would be to use the comment_author hook.

    Leave the function <?php comment_author(); ?> in your template and add this to your functions.php

    add_filter( 'comment_author', 'my_site_comment_author' );
    
    function my_site_comment_author( $author ) {
    	global $comment;
    
    	if ( $comment->user_id )
    		$author = '<a href="' . get_author_posts_url( $comment->user_id ) . '">' . $author . '</a>';
    
    	return $author;
    }
    Thread Starter asechrest

    (@asechrest)

    Ahhh, thanks Greenshady! Filters are outside my current understanding, but I’m working on getting there.

    Really appreciate this. I’m gonna go through your code piece by piece to make sure I know what it’s doing.

    With your permission, I’d like to add your block of code as an addendum to my blog post. Would have preferred not to edit the template file to begin with, but that was all I could come up with.

    P.S. – Have used the advice from your website on widgetizing the main content sections of my theme in the past. Thanks for that.

    Sure, you can use the code in your post. My code above also assumes that <?php comment_author(); ?> is being used. There are several different ways to call the comment author in WP, so the solution might not work for everyone’s theme.

    If you ever want to learn more about WP hooks, check out this site:
    http://adambrown.info/p/wp_hooks

    Thread Starter asechrest

    (@asechrest)

    Saved that link, thanks. It’s going to come in handy as I keep digging into PHP and WP.

    And just so I have this right, the primary reason(s)/advantage(s) for using the hook over adjusting the template file is what, exactly? (Other than it just seems cleaner.)

    If I auto-upgrade my theme, would this functionality remain, or would it overwrite my functions.php file?

    If this is in your theme’s functions.php, it will get overwritten with an auto-upgrade, but this is easily avoidable for the most part (except for one line of code). So, the best thing to do is put all of your custom functions into a separate PHP file. Chris Pearson has a great article explaining the benefits of a custom functions file:
    http://www.pearsonified.com/2008/05/how-to-use-wordpress-functions.php

    A great reason for this is that you can keep all of your custom stuff in one file and transfer it from theme to theme and not have to worry too much about it in the future.

    I personally prefer working in the realm of child themes, but that’s a topic for another day.

    To asechrest, I’m interested in how you solved this problem since I’ve been looking for one for days now. I checked out your blog but it’s now a parked domain. Can you post here that codes/templates you edited to make this possible? Thanks.

    I tried greenshady’s solution but it still doesn’t make the comment_author_link linked to the author profile page. That’s just what I want to happen: make the comment author’s link redirected to the author’s profile page automatically created upon signup, and not to the URL specified by the comment author.

    Anyone who can help?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Author Link in a Comment’ is closed to new replies.