Check if a taxonomy term synonym exists I have a $category variable containing country names separated by comma. So I use the following code to save them as taxonomy terms:

$tids = array(); $ctids = array();
$category = explode(',',$category);
foreach($category as $cat) {
  insert_term_if_not_exists($cat, 'countries');
  $term_country = taxonomy_get_term_by_name($cat);
  $tid_country = key($term_country);
  $lang_country = field_language('node', $node, 'field_country');
  array_push($ctids, $tid_country);
}
foreach ($ctids as $index =&gt; $ctid) { 
 $node-&gt;field_country[$lang_country][$index]['tid'] = $ctid; 
}


As you see the above code uses the insert_term_if_not_exists() function which works perfectly well to check if a taxonomy term exists and to create it programmatically if it doesn't:

function insert_term_if_not_exists($term_name, $vocabulary_name) {
   $vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_name);
   $matched_terms = taxonomy_get_term_by_name($term_name, $vocabulary-&gt;machine_name);
  if(empty($matched_terms)) {
    $term = new stdClass();
    $term-&gt;vid = $vocabulary-&gt;vid;
    $term-&gt;name = $term_name;
    taxonomy_term_save($term);
  } else {
    $term = array_shift($matched_terms);
  }
return $term;
}


However, I also need to check and avoid creating a new taxonomy term if it is a synonym of already existing another taxonomy term. So I modified the above code in the following way:

function insert_term_if_not_exists($term_name, $vocabulary_name) {
   $vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_name);
   $matched_terms = taxonomy_get_term_by_name($term_name, $vocabulary-&gt;machine_name);

    if(empty($matched_terms)) {
      $matched_synonym = synonyms_get_term_by_synonym($term_name, $vocabulary);
      if (empty($matched_synonym)) {
        $term = new stdClass();
        $term-&gt;vid = $vocabulary-&gt;vid;
        $term-&gt;name = $term_name;
        taxonomy_term_save($term);
      } else {
        /*
        * Here I need to do something, but don't know what exactly. Using
        * $term = taxonomy_term_load($matched_synonym);
        * here doesn't work.
        */
        }
    } else {
        $term = array_shift($matched_terms);
    }
  return $term;
}


Having troubles to make it work properly.
Edited my answer since my original answer referred to dev code.

The following simple module works with Drupal 7.34 and the current stable version of synonyms (7.x-1.2) and successfully matches a synonym to a taxonomy term.

synonyms_test.module:

function synonyms_test_menu() {
  $menu['test-synonyms'] = array(
    'page callback' =&gt; 'synonyms_test_page',
    'access callback' =&gt; TRUE,
  );

  return $menu;
}

function synonyms_test_page() {
  $vocabulary_name = 'tags';
  $output = '&lt;p&gt;Assuming you have a taxonomy with machine name "tags" and that '
            . 'taxonomy has a term named "first term" and that term has a '
            . 'synonym "syn one", this test will produce these results:&lt;/p&gt;';
  $test_terms = array('first term', 'bogus term', 'syn one');
  $vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_name);
  foreach($test_terms as $test_term) {
    $output .= 'Testing "' . $test_term . '"&lt;br&gt;';
    $matched_terms = taxonomy_get_term_by_name($test_term, $vocabulary-&gt;machine_name);
    if(empty($matched_terms)) {
      $output .= 'Not an existing taxonomy term.';
    } else {
      $output .= 'Is an existing taxonomy term.';
    }
    $output .= '&lt;br&gt;&lt;br&gt;';
    $matched_synonym = synonyms_get_term_by_synonym($test_term, $vocabulary);
    if(empty($matched_synonym)) {
      $output .= 'Not an existing synonym.';
    }
    else {
      $output .= 'Is an existing synonym, for tid ' . $matched_synonym;
    }
    $output .= '&lt;br&gt;=========&lt;br&gt;&lt;br&gt;';
  }
  return $output;
}


* Original answer below *

This is the function you're looking for (from synonyms.module):

/**
 * Returns the term objects that have the given string as a synonym.
 *
 * @param $synonym
 *  A string containing the synonym to search for.
 * @param $vocabulary
 *  (optional) The machine name of the vocabulary in which to search for.

 * @return
 *  An array of term objects containing that synonym.
 */
function synonyms_root_terms_get($synonym, $vocabulary = NULL) {
  $tids = synonyms_root_tids_get($synonym, $vocabulary);

  return taxonomy_term_load_multiple($tids);
}


So if your $matched_terms is empty, try something like:

$matched_synonyms = synonyms_root_terms_get($term_name, $vocabulary-&gt;machine_name);
if(empty($matched_synonyms)) {
  // ...now create your new term
}
else {
  $term = array_shift($matched_synonyms);
}