[development] programmatic creation of node

Earnie Boyd earnie at users.sourceforge.net
Wed Mar 12 11:55:15 UTC 2008


Quoting Omar Abdel-Wahab <owahab at gmail.com>:

Assuming you might want to allow for possible other users than uid 1:

> Any ideas why does this fails to set the node author:
global $user;
$user = user_load(array('uid'=>$uid); //$uid is an optional function 
parameter with a default of 1.
//If the action is a temporary switch to admin user be sure to save the 
original //value of $user so you can reset it before exiting.
> $node = new stdClass();
> $node->title = 'foo';
> $node->uid = 1;
//change to
$node->uid = $uid;
$node->name = $user->name;
> node_submit($node);
//Node submit is usually called after user clicks submit and is called to
//prepare the node for the save.  Things like converting the $node from an
//array to an object, adding uid to the node, adding the teaser to the 
node and
//invoking modules with _submit and _nodeapi hooks implemented.  If you don't
//need or want these things then don't call it.
> node_save($node);
>
> While this succeeds:
> $node = new stdClass();
> $node->title = 'foo';
> node_submit($node);
> $node->uid = 1;
> node_save($node);
>
//Node submit sets $node->uid to zero if $node->name isn't loaded by 
user_load.


Earnie -- http://for-my-kids.com/
-- http://give-me-an-offer.com/



More information about the development mailing list