0

I need to create a specific content, for instance, a Page, every 15 days.

Of course that goes into some cron job.

But how can I do that? Do I need to implement the hook_cron at my .module?

1
  • Any comments for that -1? Commented May 16, 2012 at 11:12

1 Answer 1

2

You got it! Implement hook_cron and inside that function create a node using the node_save().

Each time you run cron by going to /cron.php or each time it is called from your cron job, your hook will automatically be called.

Here's some sample code, partly borrowed from http://drupal.org/node/1111514

So your .module file should at least have the following,

// Implementation of hook_cron
mymodule_cron {

  // add node properties
  $newNode = (object) NULL;
  $newNode->type = '{NODE_TYPE}';
  $newNode->title = '{NODE_TITLE}'
  $newNode->uid = {USER_ID};
  $newNode->created = strtotime("now");
  $newNode->changed = strtotime("now");
  $newNode->status = 1;
  $newNode->comment = 0;
  $newNode->promote = 0;
  $newNode->moderate = 0;
  $newNode->sticky = 0;

  // add fields
  $newNode->field_{YOUR_CUSTOM_FIELD_1}[0]['value'] = '{DATA_1}';
  $newNode->field_{YOUR_CUSTOM_FIELD_2}[0]['value'] = '{DATA_2}';

  // save node
  node_save($newNode);
  }

After you enable your new module, just run cron by hand (go to /cron.php) and test it out to see if your node was created!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.