1

This is related to generate groups in BuddyPress.

I have a spreadsheet with (in this case) a group name, group description and slug.

I need to grab the information from the file, turn it into an array, then loop through it and call groups_create_group() every time.

I can find that function in bp-groups.php (http://www.nomorepasting.com/getpaste.php?pasteid=35217). It tells me all the parameters you need to fill in.

I'm quite new to this and looking for how I can do this. Do you know how I can grab this information and turn it into an array? An loop it through and call groups_create_group() every time?

If you have any handy links regarding this as well I'll appreciate it.

Anders
  • 633
  • 2
  • 11
  • 21

1 Answers1

2

As long as you have the function groups_create_group available (ie. The required file has been included) you should be able to do something like this

<?php

$groups = array();

if (($handle = fopen("groupData.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $group = array('group_id' => 'SOME ID', 'name' => $data[0], 'description' => $data[1], 'slug' => $data[2], 'date_created' => gmdate( "Y-m-d H:i:s" ), 'status' => 'public' );
        $groups[] = $group;
    }   
    fclose($handle);
}

foreach ($groups as $group) {
    groups_create_group($group);
} 

Note, the call to groups_create_group in the pasted code you provided was explicitly calling another method to sanitize the slug before passing it to the function. So you may want to change the assignment to the $group variable to this:

 $group = array('group_id'    => 'SOME ID', 
                'name'        => $data[0], 
                'description' => $data[1], 
                'slug' => groups_check_slug(sanitize_title(esc_attr($data[2]))), 
                'date_created' => gmdate( "Y-m-d H:i:s" ), 
                'status' => 'public' 
 );
sberry
  • 128,281
  • 18
  • 138
  • 165
  • So I should make a php-file with the code you wrote? Do I need to copy the functions from the other file in to this or will it get the functions? – Anders Dec 28 '10 at 20:11
  • I used the code (with revision) in a new file (called grupper.php), but got this message: Fatal error: Call to undefined function groups_check_slug() in /public_html/cocacpla.com/test/wp-content/plugins/buddypress/grupper.php on line 10 – Anders Dec 28 '10 at 20:25