In the last tips http://www.drupalstore.info/content/drupal-tips-saving-taxonomy-terms-id... , We learn to save the taxonomy terms id programmatically using node_save() function.

Now how about if the taxonomy terms is set to free tagging?

The situation with free tagging is a bit different than a rigid taxonomy categorizing. With free tagging, the taxonomy terms id is not available when user is allowed to create a new taxonomy terms and if your user is using "new taxonomy1, new taxonomy 2" format to enter the new taxonomy terms then the last tip won't record and create the new taxonomy entered by the user.

To handle this matter , we need to modify the code before saving the node with node_save()

// standard node save
 $newnode = new StdClass();
 $newnode->uid = $user->uid;
 $newnode->nid = '';
 $newnode->title = $form_state['values']['your_form_title_field'];
 $newnode->body = $form_state['values']['your_form_body_field'];
 $newnode->type = 'your_node_type';
 $newnode->status = '0 for not published and 1 for published';
 $newnode->promote = '0 for not to promote and 1 for promote to front page';
 $newnode->comment = '0 for no comment allowed, 1 for read only and 2 for read and write';

// this is the code for saving the free tag taxonomy terms
 $newnode->taxonomy = array('tags' => array('your taxonomy vocabulary vid number' => $form_state['values']['your free tagging form field']));
	

// invoke the node_submit() to fill out the missing node data
$node = node_submit($newnode);
	
// Save the node
node_save($node);

As a bonus, you can actually use the drupal tagging module in your custom form. with this module, invoking free tagging is much more user friendly.

To attach the new tagging form element in your module :


$form['album']['album_tagging'] = array(
 '#type' => 'tagging_widget', // this is important
 '#title' => t('Set your title'),
 '#theme' => 'tagging_widget', // you can use your own theme here but you may need to copy the theme_tagging_widget from the tagging module and modify it
 '#vid' => 'enter the vocabulary vid for the free tagging term',
 '#required' => TRUE,

/**
Now after you managed to create and add the node with taxonomy data on it, you can also edit the stored taxonomy data in the form by passing the default value of the tagging element.

You may need to convert the $node->taxonomy array into a string with comma separated for each taxonomy name
**/
 '#default_value' => taxonomy_implode_tags($node->taxonomy, 'vocabulary id number here'), // notice the use of taxonomy_implode_tags() function
);

Happy free tagging.