Skip to content

Instantly share code, notes, and snippets.

@semperos
Created October 27, 2010 14:54
Show Gist options
  • Save semperos/649181 to your computer and use it in GitHub Desktop.
Save semperos/649181 to your computer and use it in GitHub Desktop.
Create simple node programmatically
<?php
/**
* Programmatic creation of a node - as simple as it gets
*
* All Drupal nodes require a title; everything is else is optional or
* is auto-generated by Drupal upon saving the node to the database.
* This node has no extra CCK fields defined; we're simply giving it
* a title and a body. The author will be the super-admin user, and its
* published status will be "published".
*/
// Create an empty object
$node = new stdClass;
// Setup the basics
$node->uid = 1;
$node->status = 1;
$node->type = 'page';
// Fill in the data
$node->title = 'Results for submission ' . $something_unique_here;
$node->body = 'Results: ' . $body_of_results;
// Submit the node, so validation is run
$node = node_submit($node);
// Save the now-validated node to the database
node_save($node);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment