Programmatically inserting, editing and deleting a taxonomy term using drupal api is as easy as A,B,C. After scanning taxonomy_term_save() function, it looks like that if we pass an associated array as an argument to this function with proper keys we can make this happen very easily.
Inserting a taxonomy term:
<?php
$term = array(
'vid' => 5, // Voacabulary ID
'name' => 'Drupal', // Term Name
'synonyms' => 'Druplet', // (Optional) Synonym of this term
'parent' => 11, // (Optional) Term ID of a parent term
'relations' => array(15), // (Optional) Related Term IDs
);
taxonomy_save_term($term);
?>
Editing a taxonomy term:
<?php
$term = array(
'vid' => 5, // Voacabulary ID
'tid' => 17, // If we add key 'tid' to this array then the function will update this term.
'name' => 'Drupal', // Term Name
'synonyms' => 'Druplet', // (Optional) Synonym of this term
'parent' => 11, // (Optional) Term ID of a parent term
'relations' => array(15), // (Optional) Related Term IDs
);
taxonomy_save_term($term);
?>
Deleteing a taxonomy term:
<?php
$term = array(
'tid' => 17, // If only key in this array is 'tid', function will take it as a deletion request.
);
taxonomy_save_term($term);
?>
If there are more than one synonyms then you can concatenate each synonym with '\r\n'. Following is a example of inserting three synonyms, "Druplet, Drup, Ultimate Drupal".
'synonyms' => "Druplet\r\nDrup\r\nUltimate Drupal", // These are three synonyms
If there are more than one parent then you can add each parent in an array, e.g.
'parent' => array(11, 10, 13),
You can nest as many parents as you want, e.g.
'parent' => array(11, array(9, 10), 13),
If there are more than one relations to this term then you can add each relation in an array i.e.
'relations' => array(15, 16, 17),