get_user_by()
receives user data not taxonomy terms. Since you are not dealing with real userdata you’ll have to use get_term_by()
to receive the term name. You would use it like this:
$username = get_term_by( 'id', (int) $_POST['alt_comment_user'], 'custom_authors' );
Fixed code:
function taxonomy_dropdown() {
wp_dropdown_categories( array(
'name' => 'alt_comment_user',
'taxonomy' => 'custom_authors',
'hide_empty' => false
));
}
add_action( 'comment_form_logged_in_after', 'taxonomy_dropdown' );
add_action( 'comment_form_after_fields', 'taxonomy_dropdown' );
function save_user_settings( $input ) {
global $wpdb;
if( current_user_can( 'moderate_comments' ) && isset( $_POST['alt_comment_user'] ) ) {
$username = get_term_by( 'id', (int) $_POST['alt_comment_user'], 'custom_authors' );
$my_fields = array(
'comment_author' => $wpdb->escape( $username->name ),
'user_ID' => 0,
);
$input = $my_fields + $input;
}
return $input;
}
add_filter( 'preprocess_comment', 'save_user_settings' );