Last active
August 29, 2015 14:05
-
-
Save graceman9/97b0d49b2dc88f04f138 to your computer and use it in GitHub Desktop.
Drupal 7 create node programmatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Drupal 7 create node programmatically. | |
* | |
* @see https://www.drupal.org/node/1173136, important! - read comments below post. | |
* | |
* also @see http://www.lightrains.com/blog/programmatically-create-node-drupal-7, better explanation. | |
* also @see http://fooninja.net/2011/04/13/guide-to-programmatic-node-creation-in-drupal-7/ | |
*/ | |
function my_create_node($title, $body) | |
global $user; | |
$node = new stdClass(); | |
$node->type = 'page'; | |
node_object_prepare($node); | |
$node->uid = $user->uid; | |
$node->name = $user->name; | |
$node->title = $title; | |
$node->language = 'und'; | |
$node->body[$node->language][0]['value'] = $body; | |
$node->body[$node->language][0]['summary'] = ''; | |
$node->body[$node->language][0]['format'] = 'full_html'; | |
$node->comment = 1; | |
$node->status = 1; // 1 means published | |
$node->promote = 0; | |
$node->revision = 0; | |
$node->changed = $_SERVER['REQUEST_TIME']; | |
$node->created = $_SERVER['REQUEST_TIME']; | |
node_submit($node); | |
node_save($node); | |
drupal_set_message('Page '. $title .' was created.'); | |
} | |
my_create_node('Seo Training', 'Body text.'); | |
my_create_node('Social Marketing', 'Body text.'); | |
my_create_node('Development', 'Body text.'); | |
my_create_node('Optimization', 'Body text.'); | |
my_create_node('Pay Per Clicks', 'Body text.'); | |
my_create_node('Seo Tips', 'Body text.'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment