Comments do not respect display_name setting, how to make plugin to overcome this

When I post something on WordPress, it’s posted with my display name. However if I change my display name, posts posted by me will be updated with the new author. Comments will stay with the old name.

Since in my wordpress site comments are available only for registered users, and the display name field is hidden – they have custom profiles, I would like to overwrite the display_name used (it should always match first_name + last_name). So I’ve tried the the_author hook with no luck.

Read More

Looking at the source code, I found that the name of the author in author_link is generated as follows:

function get_comment_author( $comment_ID = 0 ) {
    $comment = get_comment( $comment_ID );
    if ( empty($comment->comment_author) ) {
        if (!empty($comment->user_id)){
            $user=get_userdata($comment->user_id);
            $author=$user->user_login;
        } else {
            $author = __('Anonymous');
        }
    } else {
        $author = $comment->comment_author;
    }
    return apply_filters('get_comment_author', $author);
}

This means that if the comment_author is written to the database, I cannot use it in any way. If not – the current user_login of the comment author will be passed to the get_comment_author hook, where I could query the DB to get the row and hence the first and last name. I don’t like that new query will be needed, but I can live with this if it’s the only way. But what steps I should perform to get it working. Right now the comment_author is always written to the database.

Related posts

Leave a Reply

2 comments

  1. This code does the job with a filter. Doesn’t care what the comment says the author’s name is.

    Nothing particularly tricky about it. Should be self-explanatory.

    add_filter('get_comment_author', 'wpse31694_comment_author_display_name');
    function wpse31694_comment_author_display_name($author) {
        global $comment;
        if (!empty($comment->user_id)){
            $user=get_userdata($comment->user_id);
            $author=$user->display_name;    
        }
    
        return $author;
    }
    
  2. You have to remember that each profile sets his/her own display_name setting, and in order to enforce it you can hook into the pre_user_display_name filter which gives you the chance to make changes to it before a user is updated or inserted. Unfortunately, however, pre_user_display_name does not pass any information besides display_name which is usually user_login. And you can’t even hack around with static data in your functions to wait for pre_user_first_name and pre_user_last_name as they come only after display_name.

    As you can see from the code that you posted (http://core.trac.wordpress.org/browser/tags/3.2.1/wp-includes/comment-template.php#L23) if the comment_author column is empty then it the user is fetched via get_userdata() (supplied with the user_id property of the comment), which yields the user data via a database query.

    As I understand you want to avoid the additional query (especially if get_userdata() had to be issued to get the $author), so the comment_author property of $comment (hydrated by the get_comment() call) should already contain a well-formatted display name to avoid any further queries.

    Thus, comment_author has to have itself stored into the database with in the desired format from the very moment the comment is stored.

    wp_new_comment() http://core.trac.wordpress.org/browser/tags/3.2.1/wp-includes/comment.php#L1302 is a great place to start looking, as it is called by wp-comments-post.php as soon as you post a comment.

    The very first thing that you see is the preprocess_comment filter application, so you can alter the comment_author immediately and have the altered version stored in the database.

    You have your user_ID, which you can use to get the data for the user and alter the name accordingly. To avoid the extra query for user data by the user_ID, however, you can try using the global $current_user, which is already pre-hydrated with the user metadata.

    Here’s a simple proof of concept:

    function wpse31694_alter_comment_author( $commentdata ) {
        global $current_user;
        $commentdata['comment_author'] = $current_user->user_firstname.' '.$current_user->user_lastname;
        return $commentdata;
    }
    add_filter( 'preprocess_comment', 'wpse31694_alter_comment_author' );
    

    In order to alter the comment_author data that have previously been entered, that contain the username instead of the required form you can schedule a simple Cron for late at night that will go through each and every comment one by one and alter the comment_author to form the required one. And additionally you can wp_update_user() and switch the display_name via that same Cron run, it’s up to you.