Tag Archives: node_save

Adding content programmatically in Drupal 7

Sometimes you really need to add content programmatically and here’s a little sample about the basics.

$node = new stdClass(); 

// Set content type
$node->type = 'article';

// Prepare defaults

// Define language (currently language neutral)
$node->language = LANGUAGE_NONE;

// Basic content
$node->title = 'Test';
$node->body[$node->language][0]['value'] = 'Body text';

// Example if using custom fields
$node->field_CUSTOM_FIELD[$node->language][0]['value'] = 'Value';
// Example if using fields that are taxonomy type
$node->field_CUSTOM_FIELD_TAXONOMY[$node->language][0]['tid'] = 1;

// Save node

For updating nodes load them first with:

// Node id you want to update
$nid = 1;
$node = node_load($nid);

And if you want to use revisions add the following:

$node->revision = 1;
$node->log = 'Node updated at ' . date('F j, Y, G:i');