3

I am trying to find a way to link the 'username' in comments from registered and logged in users to their profile pages instead of their website URL. And comments from unregistered users to their website URL as usual.

Is this possible? My wordpress version is 3.5.1 and I am using the default theme Twenty Twelve.

Here's the code to the function 'twentytwelve_comment':

<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
    <article id="comment-<?php comment_ID(); ?>" class="comment">
        <header class="comment-meta comment-author vcard">
            <?php
                echo get_avatar( $comment, 44 );
                printf( '<cite class="fn">%1$s %2$s</cite>',
                    get_comment_author_link(),
                    // If current post author is also comment author, make it known visually.
                    ( $comment->user_id === $post->post_author ) ? '<span> ' . __( 'Post author', 'twentytwelve' ) . '</span>' : ''
                );
                printf( '<a href="%1$s"><time datetime="%2$s">%3$s</time></a>',
                    esc_url( get_comment_link( $comment->comment_ID ) ),
                    get_comment_time( 'c' ),
                    /* translators: 1: date, 2: time */
                    sprintf( __( '%1$s at %2$s', 'twentytwelve' ), get_comment_date(), get_comment_time() )
                );
            ?>
        </header><!-- .comment-meta -->

        <?php if ( '0' == $comment->comment_approved ) : ?>
            <p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'twentytwelve' ); ?></p>
        <?php endif; ?>

        <section class="comment-content comment">
            <?php comment_text(); ?>
            <?php edit_comment_link( __( 'Edit', 'twentytwelve' ), '<p class="edit-link">', '</p>' ); ?>
        </section><!-- .comment-content -->

        <div class="reply">
            <?php comment_reply_link( array_merge( $args, array( 'reply_text' => __( 'Reply', 'twentytwelve' ), 'after' => ' <span>&darr;</span>', 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
        </div><!-- .reply -->
    </article><!-- #comment-## -->
<?php
    break;
endswitch; // end comment_type check
}
endif;
  • Do you mean the profile page as in @gdaniel 's answer below, or the "Author" page as in example.com/author/username ? – s_ha_dum Apr 4 '13 at 13:53
  • @s_ha_dum yes, I meant the author page as in example.com/author/username/ – theshorttree Apr 4 '13 at 18:42
5

I have written a solution for that some time ago:

<?php # -*- coding: utf-8 -*-
/**
 * Plugin Name: T5 Comment author URI to blog author page
 * Description: Changes the comment author URI to the blog’s author archive
 * Version:     2012.07.18
 * Author:      Fuxia Scholz
 * Author URI:  https://fuxia.me
 * License:     MIT
 * License URI: http://www.opensource.org/licenses/mit-license.php
 */

if ( ! function_exists( 't5_comment_uri_to_author_archive' ) )
{
    add_filter( 'get_comment_author_url', 't5_comment_uri_to_author_archive' );

    function t5_comment_uri_to_author_archive( $uri )
    {
        global $comment;

        // We do not get the real comment with this filter.
        if ( empty ( $comment )
            or ! is_object( $comment )
            or empty ( $comment->comment_author_email )
            or ! $user = get_user_by( 'email', $comment->comment_author_email )
        )
        {
            return $uri;
        }

        return get_author_posts_url( $user->ID );
    }
}
  • Exactly what I was looking for! You can as well just go ahead and remove the URL field if you want to avoid guest commenters linking: add_filter('comment_form_default_fields', 'remove_url'); function remove_url($val) { $val['url'] = ''; return $val; } – Christine Cooper Apr 4 '13 at 20:13
1

If the users are already logged in, then simply pointing to the admin page would have them see their profile. The link can point them to:

User profile page: http://yoursite.com/wp-admin/profile.php

You would need to edit your comments template to check if the user is logged in (if I'm not mistaken the comments template already check for that, depending if the theme was well coded). If not you can use:

is_user_logged_in()

You can get the comments author's link with:

comment_author_link()
1

Guided mostly by s_ha_dum's comment, here's how you can generate a link to author's archive page (where all posts by that author are listed). You need to put the code inside functions.php's twentytwelve_comment function, where $comment object is available.

$uname = get_the_author_meta( 'user_login', $comment->user_id ); // get username
echo site_url('/author/' . $uname);

As for comment_author_link, it will get you a link to a URL author may have provided in their profile information. If they did not then it will just return the author's name.

  • I was able to find the function you referred to but I am not sure where I should input the given code. Pasted to code in my original question as it was too long. – theshorttree Apr 4 '13 at 18:45
  • Note author is not a fixed URL part. On wpkrauts.com we use by instead. So you might create broken links. – fuxia Apr 17 '13 at 19:03

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.