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.

1 Answer
1

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="https://wordpress.stackexchange.com/questions/50661/$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');

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *