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.

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)){
        } 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.

2 Answers 2


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)){

    return $author;

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.

  • cron job does not work for me. In best case scenario, I would like not to alter the way comment data is saved. What I'm thinking of, is getting the user_id of the comment author and retrieving user data. The goal is run-time, when plugin is activated, all comments to display the first_name last_name as author. Another way is to save userid as comment_author, so I have something unique to rely on when querying the user data, but still I need to be able to modify the output. Commented Oct 21, 2011 at 10:46
  • Cron in WordPress works for everyone. Like I said, either change the display_name for every user, or the comment_author for every comment. There's really no need for those extra queries to find out the name/lastname from a user ID of a comment - too much overhead.
    – soulseekah
    Commented Oct 21, 2011 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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