I am creating a custom module for Drupal 7. I wanted to post the node that gets created of type "Article" to facebook wall so that user doesn't have to manually create a wall post.

For that I need to get a method (function) in drupal which gets called as soon as the nodes gets created, where I can put my Facebook API codes.


You are looking for hook_node_insert()

This is from API page:

Respond to creation of a new node.

This hook is invoked from node_save() after the database query that will insert the node into the node table is scheduled for execution, after the type-specific hook_insert() is invoked, and after field_attach_insert() is called.

function MyModule_node_insert($node) {

  if($node->type == 'article') {
    //your code here

function custom_func() {
  //your code goes here

Another way to achieve this goal is http://twitterfeed.com. It allows you to export RSS (generated by views) to several social networks, including Facebook.


In most cases


does the job. The hook_node_insert() is called before actual database write query so you cannot retrieve nid from that node. Another hook called after saving node is:


Available after installing module


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy