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),
Hey, thanks for this! Just
Anonymous (not verified) Mon, 2011-09-19 15:47
Hey, thanks for this! Just what i was looking for. 1 question- once we've inserted the term into our taxonomy, how can we programatically add/link that term to a node??
First of all accept my
admin Sun, 2011-10-16 05:28
In reply to Hey, thanks for this! Just by Anonymous (not verified)
First of all accept my appology for replying late, coming towards your question; If you want to programatically link a term to a node then use taxonomy.module function taxonomy_node_save($node, $terms), please check api.drupal.org for the details about this function. If need further help please post a reply to this comment.
You helped me a lot
Christian (not verified) Sat, 2012-01-07 14:20
Thank you!
Excellent
WalterWhite (not verified) Wed, 2014-09-17 03:50
I actually tried to link a term to a node using this taxonomy.module function. However, I’m not able to get the desired results. I even referred the api.drupal. It has not been of much help. http://poweredwebsite.com/ Kindly give more guidelines.
How to validate term name is not empty while creating term progr
Snehal (not verified) Fri, 2015-05-01 05:09
How to validate term name is not empty while creating term programmatically. i.e. Inserting a taxonomy term:
Great help
Bob (not verified) Wed, 2015-11-04 07:45
Thank you for this post, was looking for this for a while!