Programatically adding a Drupal 6 Node

Creating a drupal node programatically varies upon the setting of your drupal installation. Here is the basic way of creating a node via a script in Drupal 6.

			$node = (object) NULL;
			$node->type = '$NODE_TYPE'; // your node type
			$node->uid = '1'; // ID of the user
			$node->created=time();  // current time
			$node->body = $TEXT; // CONTENT	
			$node->title = $TITLE; // Title of the node			
			
			$node = node_submit($node);
			node_save($node);

Now lets define each line one by one

                        $node = (object) NULL;

This creates the node object needed to create our node

			$node->type = '$NODE_TYPE'; // your node type
			$node->uid = '1'; // ID of the user
			$node->created=time();  // current time
			$node->body = $TEXT; // CONTENT
			$node->title = $TITLE; // Title of the node

In here we put the parameters of the node. The structure shown above are what is important in creating a node, depending on the modules installed or your CCK configuration you might need to change this. In order to check the complete structure of the your node is to display a print_r($node) in your page ( don't forget to remove it afterward ) .

			$node = node_submit($node);
			node_save($node);

This part actually saves the node into the database, after this code is executed the node should be visible.

Comments