BuddyPress: hook to add new profile fields and groups

I am writing a plugin that adds additional profile group and some fields in it when activated. I am using basic $wpdb functions to insert new rows into bp_xprofile_groups and bp_xprofile_fields

So the whole process looks very lame:

Read More
  1. I add new row to bp_xprofile_groups;
  2. I get ID of that row (WHERE name = ‘New Group Name’);
  3. I use these ID to insert new rows to bp_xprofile_fields

Some fields are options so in total I make 5 SQL queries just to create 1 group field and 4 option fields with 2 checkboxes in that field group.

I would like to make it using BuddyPress hooks. I found what I need:

xprofile_insert_field_group($args) and xprofile_insert_field($args)

so it looks like this:

$args = array(
    'name'           => 'Test',
    'description'    => '',
    'can_delete'     => '0'
    );
xprofile_insert_field_group( $args );

and then I am suppose to use xprofile_insert_field() but I do not know ID of a group I just created to use in field_group_id:

$args = array(
    'field_group_id'           => ?????,
    'parent_id'    => '',
    'type'     => 'textbox',
    ...
    ...
    );
xprofile_insert_field( $args );

Does anyone have any suggestions?

Related posts

Leave a Reply

1 comment

  1. Well seems like not many people are digging into BP so here is the solution I found.

    To add a new field group using BP hooks:

    // Create new Field Group       
    $group_args = array(
         'name' => 'Social Networks'
         );
    $group_id = xprofile_insert_field_group( $group_args ); // group's ID
    

    To add some fields to this new group:

    // Insert New Field
    xprofile_insert_field(
        array (
               field_group_id  => $group_id,
               name            => 'Twitter',
               can_delete      => false, // Doesn't work *
               field_order  => 1,
               is_required     => false,
               type            => 'textbox'
        )
    );
    

    I think there is currently a bug in BP. can_delete = 0 means you cannot delete something (ie field or group), but no matter what you pass to xprofile_insert_field() it is going to be can_delete = 1.