4.6.x node.module node_save($node)
4.7.x node.module node_save(&$node)
5.x node.module node_save(&$node)
6.x node.module node_save(&$node)
7.x node.module node_save($node)

Saves changes to a node or adds a new node.


$node: The $node object to be saved. If $node->nid is omitted (or $node->is_new is TRUE), a new node will be added.

26 calls to node_save()
book_admin_edit_submit in modules/book/book.admin.inc
Form submission handler for book_admin_edit().
callback_batch_operation in modules/system/form.api.php
Perform a single batch operation.
callback_queue_worker in modules/system/system.api.php
Work on a single queue item.
CommentInterfaceTest::setEnvironment in modules/comment/comment.test
Re-configures the environment, module settings, and user permissions.
CommentRSSUnitTest::testCommentRSS in modules/comment/comment.test
Test comments as part of an RSS feed.

... See full list

1 string reference to 'node_save'
menu_test_menu in modules/simpletest/tests/menu_test.module
Implements hook_menu().


modules/node/node.module, line 1077
The core that allows content to be submitted to the site. Modules and scripts may programmatically submit nodes using the usual form API pattern.


function node_save($node) {
  $transaction = db_transaction();
  try {

    // Load the stored entity, if any.
    if (!empty($node->nid) && !isset($node->original)) {
      $node->original = entity_load_unchanged('node', $node->nid);
    field_attach_presave('node', $node);
    global $user;

    // Determine if we will be inserting a new node.
    if (!isset($node->is_new)) {
      $node->is_new = empty($node->nid);

    // Set the timestamp fields.
    if (empty($node->created)) {
      $node->created = REQUEST_TIME;

    // The changed timestamp is always updated for bookkeeping purposes,
    // for example: revisions, searching, etc.
    $node->changed = REQUEST_TIME;
    $node->timestamp = REQUEST_TIME;
    $update_node = TRUE;

    // Let modules modify the node before it is saved to the database.
    module_invoke_all('node_presave', $node);
    module_invoke_all('entity_presave', $node, 'node');
    if ($node->is_new || !empty($node->revision)) {

      // When inserting either a new node or a new node revision, $node->log
      // must be set because {node_revision}.log is a text column and therefore
      // cannot have a default value. However, it might not be set at this
      // point (for example, if the user submitting a node form does not have
      // permission to create revisions), so we ensure that it is at least an
      // empty string in that case.
      // @todo: Make the {node_revision}.log column nullable so that we can
      // remove this check.
      if (!isset($node->log)) {
        $node->log = '';
    elseif (!isset($node->log) || $node->log === '') {

      // If we are updating an existing node without adding a new revision, we
      // need to make sure $node->log is unset whenever it is empty. As long as
      // $node->log is unset, drupal_write_record() will not attempt to update
      // the existing database column when re-saving the revision; therefore,
      // this code allows us to avoid clobbering an existing log entry with an
      // empty one.

    // When saving a new node revision, unset any existing $node->vid so as to
    // ensure that a new revision will actually be created, then store the old
    // revision ID in a separate property for use by node hook implementations.
    if (!$node->is_new && !empty($node->revision) && $node->vid) {
      $node->old_vid = $node->vid;

    // Save the node and node revision.
    if ($node->is_new) {

      // For new nodes, save new records for both the node itself and the node
      // revision.
      drupal_write_record('node', $node);
      _node_save_revision($node, $user->uid);
      $op = 'insert';
    else {

      // For existing nodes, update the node record which matches the value of
      // $node->nid.
      drupal_write_record('node', $node, 'nid');

      // Then, if a new node revision was requested, save a new record for
      // that; otherwise, update the node revision record which matches the
      // value of $node->vid.
      if (!empty($node->revision)) {
        _node_save_revision($node, $user->uid);
      else {
        _node_save_revision($node, $user->uid, 'vid');
        $update_node = FALSE;
      $op = 'update';
    if ($update_node) {
        'vid' => $node->vid,
        ->condition('nid', $node->nid)

    // Call the node specific callback (if any). This can be
    // node_invoke($node, 'insert') or
    // node_invoke($node, 'update').
    node_invoke($node, $op);

    // Save fields.
    $function = "field_attach_{$op}";
    $function('node', $node);
    module_invoke_all('node_' . $op, $node);
    module_invoke_all('entity_' . $op, $node, 'node');

    // Update the node access table for this node.

    // Clear internal properties.

    // Clear the static loading cache.

    // Ignore slave server temporarily to give time for the
    // saved node to be propagated to the slave.
  } catch (Exception $e) {
    watchdog_exception('node', $e);
    throw $e;


Reno Greenleaf’s picture

Probably functions like this should return a value. For example, TRUE on success, FALSE otherwise. Or resulting node object.

Amir Simantov’s picture

Was just looking the documentation for that exactly...

m4olivei’s picture

The $node object passed in should be an object. Objects are implicitly passed by reference in PHP. Therefore, after node_save is run, $node is populated with all the stuff that was done to it. No need for a return value. This stumpted me at first too, until I did a quick run through with a debugger.

fuzzy76’s picture

Unless you are updating an existing node...

stickywes’s picture

Update or new, you can detect changes just fine. Save a vid before node_save() or just save a copy and then check for changes on specific properties (or all of them).

Amarjit’s picture

We can be sure that the node_save() was successful by checking the 'nid' property of the $node object.

  // Assign properties, etc.
  // Save node.
  // node_save() does not return a value. It instead populates the $node object. Thus to check if the save was successful, we check the nid.
  $node_id = $node->nid;

  return $node_id;
jpschroeder’s picture

Ah! great point. This stumped me for a while.

Still...its silly that this (and other basic drupal functions) don't return a proper value. It seems to violate some basic multi-lingual receipt conventions.

q0rban’s picture

I'm not sure you can rely on the existence of $node->nid alone, as it's possible that the process failed on an existing node object (where $node->nid already existed). I think you should instead wrap your node_save() in try/catch:

try {
  $success = TRUE;
catch (Exception $e) {
  // Do your error handling here.

kenorb’s picture


watchdog('foo', $e, array(), WATCHDOG_ERROR)
nd987’s picture

Very surprising node_save doesn't return a boolean...

davidwhthomas’s picture

The node object is passed by reference.

After saving, the $node variable will have its $node->nid value set.

valera.rozuvan’s picture

Suppose you created a custom node type called 'node_example'. It contains an image field called 'node_example_image' with 'cardinality' set to 5 (can upload 5 images). To create a node of type 'node_example' and upload 5 images (the images are taken from your custom module directory; image1.jpg, image2.jpg, ...), you can do something along the lines of:

  // Create a node object, and add node properties.
  $newNode = (object) NULL;
  $newNode->type = 'node_example';
  $newNode->uid = 0;
  $newNode->created = strtotime("now");
  $newNode->changed = strtotime("now");
  $newNode->status = 1;
  $newNode->comment = 0;
  $newNode->promote = 0;
  $newNode->moderate = 0;
  $newNode->sticky = 0;
  $newNode->language = 'und';


  for ($i = 0; $i  1,
      "uri" => $file_path,
      "filemime" => file_get_mimetype($file_path),
      "status" => 1

    $file = file_copy($file, 'public://');
    $newNode->node_example_image['und'][$i] = (array)$file;

  // Save the node.
  // The node will also get published, if $newNode->status was set to 1.
Sleavely’s picture

The main reason you cannot specify a nid in the default flavor of Drupal is because the {node}.nid column in the database is auto-incremental.

To build a workaround in your code for this may very well affect Drupal's database-wrapper's ability to properly handle autoincrements at all.

The only reason I can think of that requires a specific NID would be if you want to maintain a specific URL. In this case I would suggest using URL Aliases instead.

bailey86’s picture


I agree about the need to be careful when setting ID values. The reason I needed to do this was as part of my module https://drupal.org/project/data_export_import - for that module I needed to recreate nodes, terms and users with the same ID as when they were exported.

We're at 5,000 downloads and no real bugs to speak of - so it's looking OK - but I agree that this area has to be handled carefully.

stkrzysiak’s picture

So, I have a form and my submit handler creates nodes. After the nodes are created I have a call to drupal_mail in that handler. Now, the interesting part, I also have a node_insert hook that redirects users when that node type is created. I now realize this is a bad place for my redirect, and I can just put it in my submit handler, but I never though that a drupal_goto would prevent the remainder of my submit hook to complete. In fact, I still don't really believe it, maybe something else I am doing?

pseudocode follows:

function myform_submit() {
....some node preparation.....

function myform_node_insert() {

The drupal_mail will never fire. I guess after having written it out, it makes some sense, if you set your redirect, it can be assumed you want to abandon the script(function).

Also, remember to do node_save(&$node) to get your nid everyone(when drupal starts using php 5.4 this will change in node_save(&$node)!

stkrzysiak’s picture

Objects are passed by reference in PHP 5

ging74ru’s picture

I use hook_node_update() and hook_node_insert() to write some extra data about node to external service (mongo in my case).

What should I do to delete this extra data on node_save transaction rollback?

taylormade415’s picture


ws.agency’s picture

This function is deprecated in Drupal 8. Instead you can use Node::create

use Drupal\node\Entity\Node;

$node = Node::create([
      'type' => 'page',  //$node->type
      'title' => 'title',  //$node->title
]); //This will just create node object but won't save it!

//Add values for custom fields
$node->some_custom_field->value = 'some value';

//Finally save the node

markdatter’s picture

I just noticed that when I modify the fields of an existing node in the node object, the changes are saved to the node without calling node save. Example:

$node = node_load($nid);
$node->field_node_field[LANGUAGE_NONE][0]['value'] = 'new value';

$updated_node = node_load($nid);
$field_value = $updated_node->field_node_field[LANGUAGE_NONE][0]['value'];

After running the above code, $field_value == 'new value'.

I thought that I needed to call node_save before the changes would be saved to the database?

jeromewiley’s picture

I need to save an integer to a multi-value custom field which belongs to a node. Great to hear all I need to do is invoke the $node->field_node_field[LANG][0]['value'] = 'new value';

Will try this now and report back.

Well, I had to use:

... code up here ...
$node->field_name['und'][0]['target_id'] = $var;
field_attach_update('node', $node);
... more code down here   

... in order to get mine to work. Just using the object notation on the $node->bla_bla didn't work. However, I did not need to use node_save()

I did encounter an issue with using ['und'][0]['value'] and had to use ['target_id'], I think maybe I was working with an Entity Reference field.

CapitaineToinon’s picture

Be careful when saving a node, if not specified, node_save() will automatically set the status back to 0

$node = node_load($nid);
$node->status = 1;
// Later in your code...
$node = node_load($nid)
// For example :
$node->promote = 1;
// $node->status is now equal to 0 again
kari.kaariainen’s picture

Definitely shouldn't happen. Can't reproduce. You are missing one semicolon (which doesn't explain your experience, though).

emerham’s picture

Yeah there is a missing semicolon in your second node_load($nid). Should look like

$node = node_load($nid);
$node->status = 1;
// Later in your code...
$node = node_load($nid);
// For example :
$node->promote = 1;
// $node->status is now equal to 0 again