How to check if the current user is a comment author in WordPress

If you want to check whether the current logged-in user is the comment author for the current comment within comments loop, you can use "get_comment_author();". Please note that it seems that it displays the display name of the current comment author. Therefore, to check if the current user is a comment author, it's needed to compare the display name of the current user with the comment author:

global $current_user;
get_currentuserinfo();
if(is_user_logged_in()) {
if($current_user->display_name == get_comment_author()) {
// your code here
} else {
// your code here
}
}

Reference - get current user

$current_user = wp_get_current_user();
/**
* @example Safe usage: $current_user = wp_get_current_user();
* if ( !($current_user instanceof WP_User) )
*     return;
*/
echo 'Username: ' . $current_user->user_login . '<br />';
echo 'User email: ' . $current_user->user_email . '<br />';
echo 'User first name: ' . $current_user->user_firstname . '<br />';
echo 'User last name: ' . $current_user->user_lastname . '<br />';
echo 'User display name: ' . $current_user->display_name . '<br />'; // Display Name of the current user
echo 'User ID: ' . $current_user->ID . '<br />'; // User ID of the current user

Leave a Comment