Programmatically crate a node

Public

While I have seen others this is the method I typically use for making a node in code (programmatically)

Get raw version
php
  1. global $user;
  2. $user = user_load($user->uid);
  3. $node = new stdClass();
  4. $node->title = $user->name . ' created this';
  5. $node->type = '<node_type>';
  6. node_object_prepare($node);
  7. $node->uid = $user->uid;
  8. $node->field_custom_field['und'][0]['value'] = 'My Custom Value';
  9. $node = node_submit($node);
  10. node_save($node);
  11. $destination = '';
  12.  
  13. drupal_set_message($node->type . ' created by ' . $user->name);
  14.  
  15. if(!empty($destination)) {
  16. $destination = 'my/destination';
  17. drupal_goto($destination);
  18. }