Skip to content

Instantly share code, notes, and snippets.

@NickTomlin
Created October 10, 2012 21:24
Show Gist options
  • Save NickTomlin/3868508 to your computer and use it in GitHub Desktop.
Save NickTomlin/3868508 to your computer and use it in GitHub Desktop.
Programatic node creation in Drupal 7
<?php
/* ==========================================================================
Bootstrap Drupal
========================================================================== */
define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
/* ==========================================================================
Create A node
========================================================================== */
$node = new stdClass(); // create a new node object
$node->nid = NULL;
$node->is_new=TRUE;
$node->type = "page";
// Content
$node->title = "A programmatic node";
$node->body['und'][0]['value'] = "<h2>$bodyHeadline</h2> $bodyText";
$node->body['und'][0]['format'] = 'full_html';
// Menu * not working : (
$node->menu = array(
'enabled' => 1,
'mlid' => 0,
'module' => 'menu',
'hidden' => 0,
'has_children' => 0,
'customized' => 0,
'options' => array(),
'expanded' => 0,
'parent_depth_limit' => 8,
'link_title' => "Test Menu Item",
'description' => '',
'parent' => 'main-menu:0',
'weight' => 0,
'plid' => 0,
'menu_name' => 'menu-menu'
);
// Set some final defaults, then create the node
$node->language = LANGUAGE_NONE; // Avoid language errors
$node->uid = 1;
node_object_prepare($node); // Set some default values.
$node = node_submit($node); // Prepare node for a submit
node_save($node);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment