4

I wasn't exactly sure how to word the question, so I'm sure that's why I cannot find what I need when searching Google either.

I want to link all commenter names to their author.php page if they have edit_posts capability (Authors, Editors and Admins), otherwise link to the website they have listed in their profile.

I am not experienced with using comment_author.

Thanks.

5
+100

Edited my answer since the original code was slightly flawed. Tested and triplechecked this to make sure it does exactly what you've requested. :)

Enjoy!

function comment_author_profile_link(){

/* Get the comment author information */

global $comment;
$comment_ID = $comment->user_id;
$author = get_comment_author( $comment_ID );
$url = get_comment_author_url( $comment_ID );

/* Check if commenter is registered or not */
switch ($comment_ID == 0) {

case true: 
/* Unregistered commenter */    

    if ( empty( $url ) || 'http://' == $url )
        $return = $author;
    else
        $return = "<a href='$url' rel='external nofollow' class='url' target='_blank'>$author</a>";

break;

case false:
    /* Registered Commenter */      

    $registeredID = get_userdata($comment_ID);
    $authorName = $registeredID->display_name;
    $authorLevel = $registeredID->user_level;
    $authorURL = $registeredID->user_url;
    $authorID = $registeredID->ID;

        /* Check if they have edit posts capabilities & is author or higher */

    if ($authorLevel > 1 && user_can($authorID,'edit_posts') == true && count_user_posts($authorID) > 0) {
    /* Author+ with Posts */

    $return = '<a href="'.home_url().'/?author='.$authorID.'">'.$authorName.'</a>';

    } else {
    /* Below Author */

    if ( empty( $authorURL ) || 'http://' == $authorURL )
        $return = $authorName;
    else
        $return = "<a href='$authorURL' rel='external nofollow' class='url' target='_blank'>$authorName</a>";

    }

break;
}

return $return;
}

add_filter('get_comment_author_link', 'comment_author_profile_link');
| improve this answer | |
  • This code almost works. When pasted (as written) the commenter name for registered users is always displayed as "Anonymous". Also, I want ONLY registered users with edit_posts capability to link to the profile page. This is because I do not require registered users to fill out any profile info, however, Authors+ are required. I would rather send people straight to the commenters website, rather than an empty profile page. – Travis Pflanz May 5 '12 at 5:17
  • Fixed it now. Should be working as expected, @TravisPflanz – akamaozu May 5 '12 at 11:58
  • Beautiful! I will pay the bounty when it becomes available later this afternoon. – Travis Pflanz May 5 '12 at 13:52

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