Create node with php in Drupal 7

January 29th, 2012 — 6:39am

Creating a node with PHP in Drupal 7 is easy. This can be used to insert one node from a custom form or to insert a batch of nodes from an array of data.

Just follow these instructions to populate a node with PHP.

// start with a blank object
$node = new stdClass();
$node->type = 'node-type';
// adds defaults for node type
node_object_prepare($node);

$node->title = 'Any title';
// set language to default 'und'
$node->language = LANGUAGE_NONE;

// sets node author (can use $node->name instead)
$node->uid = 1;

// set the body field
$node->body[$node->language][0]['value'] = 'text';
$node->body[$node->language][0]['format'] = 'filtered_html';

// most other fields can be added this way
$node->field_name[$node->language][0]['value'] = 'value';

// updates author and publish date
node_submit($node);
node_save($node);
FacebookTwitterRedditDeliciousDiggMixxStumbleUponShare

Category: Programming | Tags: , One comment »

One Response to “Create node with php in Drupal 7”

  1. Barbara

    Thanks a lot for the blog post. Really looking forward to read more. Much obliged.


Leave a Reply



Back to top