The steps for programmatically creating a node are:
- Create a PHP object representing the node data
- Save the object using the node_save() function
While the mechanics are simple, there is an important responsibility involved. The Drupal work flow does data validation before calling the node_save() function; node_save() does no validation. By calling node_save() directly your code takes the responsibility for providing valid data.
Drupal 7 Changes
A quick note for those of you familiar with Drupal 6. You'll notice two changes in Drupal 7:
Body Field Is No Longer Special
In Drupal 6 the body field was special. Specifically, it had a different data structure than other fields and it always existed, even if it wasn't used. With Drupal 7 the body field is a standard field provided by core and is truly optional.
Language specification is required for the node and some fields.
The format of the text field filter is now specified by machine name, not integer. For example: full_html, filtered_html, or plain_text. This is great news for moving contents between different Drupal systems.
Basic Node Creation
The following code assumes an unchanged Drupal 7 Standard installation and will create a Drupal 7 article node.