add users to og group via vbo doesn't work: record is deleted immediately I like to add users to an OG group using vbo, a custom action and og_group().

I build up a page with a view of users to add (/node/%nid/add_user).

On this view I added a views bulk operations field with the "skip batch" option. (I skipped the batch to get the current %nid from the url).

In the custom code I like to add the selected user to the page shown. I know I can do this also with the standard group forms, but I want to do it this way :-)

So here is my code to subscribe the user to the group:

function mymodule_vbo_actions_users_woform(&amp;$user_node, $context) {
  global $user;
  //dpm($node); dpm($context);return;
  switch ($context['settings']['action_type']) {
    case 'add_user_to_group':
      $nid_page = menu_get_object();
      if (!isset($nid_page)) $nid_page = arg(1);
      $nid_page = $context['page_nid'];
      $account = user_load($user_node-&gt;uid);
      //dpm($nid_page);
      $node_page = node_load($nid_page);
      dpm($user_node);
      dpm($account);
      dpm($node_page);

      $aValues = array('entity_type'=&gt;'user','entity'=&gt;$account,'membership type' =&gt; 'OG_MEMBERSHIP_TYPE_DEFAULT',);
      $og_membership = og_group('node',$node_page-&gt;nid,$aValues,FALSE);
      $og_membership-&gt;save();
      dpm($og_membership);

      // get member roles id for selected group bundle
      $result_roles = db_query("SELECT rid FROM og_role o where group_bundle = :bundle and name=:name and gid=:gid;",array(':bundle'=&gt;$node_page-&gt;type,':name'=&gt;'bidder', ':gid'=&gt;$node_page-&gt;nid));
      $role_id = NULL;
      foreach ($result_roles as $key_roles =&gt; $value_roles) {
        $role_id = $value_roles-&gt;rid;
        break;
      }
      dpm($role_id);
      og_role_grant('node',$node_page-&gt;nid,$account-&gt;uid,$role_id);
      break;
  }
}


This code seems to be correct. I watched the og_membership table and the record for the selected user is inserted, but then DELETED immediately!?

So the question is of course: Why is the record deleted?
You might need to grant a OG role to the new member after adding them.

e.g. og_role_grant('node', $node_page-&gt;nid, $account-&gt;uid, 2);

The magic number on the end determines the type of role granted. 1 = non-member, 2 = member, 3 = administrator member