Wordpress - Post comment as different user than logged in

Yes, add an user-select field in the comment form:

add_action('comment_form_after_fields', function(){

  // allow only users who can moderate comments to do this
  if(!current_user_can('moderate_comments'))
    return;

  $user = wp_get_current_user();

  wp_dropdown_users(array(
    'name'     => 'alt_comment_user',
    'selected' => $user->ID,
  ));

});

When the form gets processed, check if an user has been selected and change the comment data before it gets inserted in the database:

add_filter('preprocess_comment', function($input){

  if(current_user_can('moderate_comments') && isset($_POST['alt_comment_user'])){    

    $user = get_user_by('id', (int)$_POST['alt_comment_user']);

    $my_fields = array(
      'comment_author'       => empty($user->display_name) ? $user->user_login : $user->display_name,
      'comment_author_email' => $user->user_email,
      'comment_author_url'   => $user->user_url,
      'user_ID'              => $user->ID,
    );

    // escape for db input
    foreach($my_fields as &$field)
      $field = $GLOBALS['wpdb']->escape($field);

    $input = $my_fields + $input;   
  }  

  return $input;
});

ref: wp_dropdown_users

Tags:

Comments

Users