Drupal: How to Create Taxonomy Term via Drupal API
Problem: you need to create terms of a certain taxonomy vocabulary in Drupal module.
Solution:
To create taxonomy term in a module, you should add something like the following code to “_insert”-functions:
$vid = variable_get(‘node_product_vocabulary’, ”); // vocabulary ID is hard coded for this example
$autoterm = array(
‘name’ => $node->title, // or whatever you want the auto-term to be named
‘parent’ => 0,
‘vid’ => $vid,
);
taxonomy_save_term($autoterm);
$new_tid = $autoterm[‘tid’];
$node->taxonomy[$vid][$tid] = $new_tid;