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)){
$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.
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.
You have to remember that each profile sets his/her own
display_name
setting, and in order to enforce it you can hook into thepre_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 besidesdisplay_name
which is usuallyuser_login
. And you can’t even hack around withstatic
data in your functions to wait forpre_user_first_name
andpre_user_last_name
as they come only afterdisplay_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 theuser
is fetched viaget_userdata()
(supplied with theuser_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 thecomment_author
property of$comment
(hydrated by theget_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 bywp-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 thecomment_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 theuser_ID
, however, you can try using theglobal $current_user
, which is already pre-hydrated with the user metadata.Here’s a simple proof of concept:
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 thecomment_author
to form the required one. And additionally you canwp_update_user()
and switch thedisplay_name
via that same Cron run, it’s up to you.