Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
The entire url is passed to the l() function which incorrectly encodes query strings, breaking the url. This can be resolved by changing the two occurrences of:
$html = l($display_term, $url, array('title' => "reference on $display_term", 'target' => $target));
to:
$url = explode('?', $url);
$html = l($display_term, $url[0], array('title' => "reference on $display_term", 'target' => $target, 'query' => $url[1]));