只要您有可用的功能 groups_create_group (即已包含所需的文件),您应该能够执行类似的操作
<?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);
}
请注意,您提供的粘贴代码中对 groups_create_group 的调用是在将 slug 传递给函数之前显式调用另一种方法来清理 slug。因此,您可能希望将$group
变量的赋值更改为:
$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'
);