Search code examples
wordpressbuddypress

Function to create a Category for each Group (BuddyPress) on creation


I'm looking to have a category created for each BuddyPress Group when it's made. So far I've found this code which would create a Category once the Group was created. I would use this code in the Theme Functions.

function example_insert_category() {
  wp_insert_term(
    'Example Category Name',
    'category',
    array(
      'description' => 'This is an example category.',
      'slug'    => 'example-category-slug'
    )
  );
}
add_action('groups_group_create_complete', 'example_insert_category');

Firstly am I on the right track. Secondly how would I set the Category Name and Category Slug using the newly created Group Name and Group Slug?

Any help would be appreciated


Solution

  • Try:

    function example_insert_category($group_id) {
      $group = groups_get_group( array( 'group_id' => $group_id) );
      //var_dump( $group ); 
      wp_insert_term(
        $group->name,
        'category',
        array(
          'description' => $group->description,
          'slug'    => $group->slug
        )
      );
    }
    add_action('groups_group_create_complete', 'example_insert_category');