I want to know how i can create taxonomy term programmatically.

I have this code

$term = new stdClass();
$term->name = ‘Term Name’;
$term->vid = 1; // ‘1’ is a vocabulary id you wish this term to assign to
$term->field_custom_field_name[LANGUAGE_NONE][0]['value'] = ‘Some value’; // OPTIONAL. If your term has a custom field attached it can added as simple as this
taxonomy_term_save($term); // Finally, save our term

From Link

I don't know where to write it. I think it will be in template.php with some hook ?

Drupal 7

  • What event are you wishing to react to?
    – Clive
    Commented Jul 22, 2013 at 14:05
  • Just to import through code some taxonomy terms so i don't have to do it via GUI. Commented Jul 22, 2013 at 14:20

1 Answer 1


If you just want to run an arbitrary PHP script through Drupal, the easiest way is probably using the Devel module

It provides a page (/devel/php) where you can copy/paste PHP code and have it run inside a fully bootstrapped instance of Drupal.

  • This is awesome! Commented Jul 22, 2013 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.