How can I programmatically flag a user? I am creating a new user programmatically in drupal7. At the time of user creation is there any way to flag the user automatically.
I am using flags module.
I tried in the hook_user_presave
function MODULE_NAME_user_presave(&amp;$edit, $account, $category) {
$flag = flag_get_flag('name_of_the_flag');
$flag-&gt;flag('flag', $account-&gt;uid);
}
Have you checked that $account-&gt;uid exists? I'm not sure that a new user would have a UID inside of hook_user_presave(). You may want try setting the flag in hook_user_insert() instead. Also worth making sure that users are allowed to be flagged by your flag type.