I am using flag module to bookmark the content. Authenticated user can normally flag the content. For anonymous user when they click on flag link it should go to login page. After successful login it should flag the node automatically.

I have the code for flagging the content, but I could find how to flag after login.

$flag = flag_get_flag('wishlist');
$flag->flag('flag', $nid, user_load($uid), TRUE);

1 Answer 1


You can try using hook_node_view for doing this. What it does is:

Act on a node that is being assembled before rendering.

Here is an idea to try out:

 * Implements hook_node_view
function yourmodule_node_view ($node, $view_mode, $langcode) {

  // use a one-timer boolean for this action
  if(user_is_logged_in() && !isset($_SESSION['autoflagging_once']))
    $_SESSION['autoflagging_once'] = true; 
    $flag = flag_get_flag('wishlist');
    $flag->flag('flag', $node->nid, user_load($uid), TRUE);


Good luck!

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.