How do I programmatically create a simple 'page' node? I have already found Drupal 6 documentation on how to programmatically create a page node.
How is it possible to achieve that in Drupal 7?



I've changed my strategy, and I'm using another approach, but I'm getting the following error:


  Fatal error: Cannot access empty property in C:\...\...\...\...\modules\field\field.attach.inc on line 198


I'm using the following code:

$form_state = array();
module_load_include('inc', 'node', 'node.pages');

$node = array('type' =&gt; 'page');
$form_state['values']['uid'] = 1;
$form_state['values']['name'] = 'admin';

$form_state['values']['title'] = 'Programatic Page';
$form_state['values']['language'] = 'en';

$form_state['values']['body']['en'][0]['value'] = 'My own value';
$form_state['values']['body']['en'][0]['summary'] = 'My own summary';
$form_state['values']['body']['en'][0]['format'] = 'filtered_html';

$form_state['values']['menu']['enabled'] = 1;
$form_state['values']['menu']['link_title'] = 'Auto-page';
$form_state['values']['menu']['description'] = 'Own Programatic Page';
$form_state['values']['menu']['parent'] = 'main-menu:0';
$form_state['values']['menu']['weight'] = 5;

$form_state['values']['path']['alias'] = 'auto-page';
$form_state['values']['comment'] = 1;

$form_state['values']['status'] = 1;
$form_state['values']['promote'] = 0;
$form_state['values']['revision'] = 0;

$form_state['values']['op'] = t('Save');

drupal_form_submit('page_node_form', $form_state, $node);


Is there anything wrong?



Using Firebug (Mozilla Firefox plugin), I figured out the name of the elements and then fill it with the proper values.
Creating a 'page' node programmatically, in Drupal 7, is done using the following code:

global $user;

$customNode = new stdClass();

$customNode-&gt;type = 'page';
node_object_prepare($customNode);
$customNode-&gt;uid = $user-&gt;uid;
$customNode-&gt;name = $user-&gt;name;

$customNode-&gt;title = 'Your page title';
$customNode-&gt;language = 'en';
$customNode-&gt;body[$customNode-&gt;language][0]['value'] = 'Your value';
$customNode-&gt;body[$customNode-&gt;language][0]['summary'] = 'Your summary';
$customNode-&gt;body[$customNode-&gt;language][0]['format'] = 'filtered_html';

$customNode-&gt;menu['enabled'] = 1;       // 1 to enable providing a link in main menu
$customNode-&gt;menu['link_title'] = 'Your page link title';
$customNode-&gt;menu['description'] = 'Your page description';
$customNode-&gt;menu['parent'] = 'main-menu:0';
$customNode-&gt;menu['weight'] = 2;

$customNode-&gt;path['alias'] = 'Your Alias';
$customNode-&gt;comment = 1;

$customNode-&gt;status = 1;        // 1 means published
$customNode-&gt;promote = 0;
$customNode-&gt;revision = 0;

$customNode-&gt;changed = $_SERVER['REQUEST_TIME'];
$customNode-&gt;created = $_SERVER['REQUEST_TIME'];

node_submit($customNode);
node_save($customNode);


This not completely functioning.
The page is being created successfully with all the values, but the link is not shown in the main menu navigation (I mean in the main menu tabs near the Home tab).
I checked it out in the created content and it's there, accessed it via "edit" button, pressed "save" (without editing anything), and it appears in the main menu.

I want it to automatically show when it is created. Do you have any idea?
The Devel project includes a module called Devel Generate for generating dummy content. You could use it as an example. I think the parts you'd need are in the devel_generate.inc file.