Q: How can I programmatically create or add new Taxonomy Terms using PHP code? A: Call the function taxonomy_save_term() passing in the details of the new taxonomy term.
To create a new taxonomy term through PHP you can use taxonomy_save_term(&$form_values) which is mainly intended for use as a helper function to by the real drupal taxonomy add term form. The relevant term details are passed in via the $form_values parameter and the Term Id of the newly created taxonomy term is placed back in this array and can be accessed by the calling code. I wrote the following function wrap taxonomy_save_term(&$form_values) to make it a bit easier to use:
function add_taxonomy_term($name, $vid, $description = '', $weight = 0) {
It is passed the name of the new term, the Id of the vocabulary under which it is to be created and optionally a description and a weight. The Id of the new term is returned.
I thought that perhaps somebody might find this code useful, and ill find it here if ever I need it again! ;-)
Did you find this post helpful? If so, we'd love to hear from you in our comments! If you found it really useful we'd love a link from your Blog-Web Site, to us!
I thought that perhaps somebody might find this code useful, and ill find it here if ever I need it again!
You cannot be wrong! ;)