Skip to:
Content
Pages
Categories
Search
Top
Bottom

Function to create a Category for each Group on creation


  • ugd
    Participant

    @ugd

    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

Viewing 8 replies - 1 through 8 (of 8 total)

  • pandreas
    Participant

    @pandreas

    Did you find any solution with this?

    I also have similar problem and want to use a category for each group creation..

    Regards,

    Andreas


    shanebp
    Moderator

    @shanebp

    Try:

    function example_insert_category( $group_id ) {
    
      $group = groups_get_group( array( 'group_id' => $group_id ) );
      $name = $group->name; 
      $slug = $group->slug; 
    
      wp_insert_term(
        $name,
        'category',
        array(
          'description' => 'This is an example category.',
          'slug'    => $slug
        )
      );
    }
    add_action('groups_group_create_complete', 'example_insert_category', 1, 1);

    pandreas
    Participant

    @pandreas

    This works great! Thank you very much for your support.

    When I choose to edit group and change, for example, the group name, of course category name doesn’t change. How can we do that?

    Thank you in advance!

    Regards,

    Andreas


    sharmavishal
    Participant

    @sharmavishal

    Following this topic


    pandreas
    Participant

    @pandreas

    Is there any answer to this? How we will tell buddypres to change category name when a group name is edited?

    Thank you much!

    Regards,

    Andreas


    shanebp
    Moderator

    @shanebp


    valuser
    Participant

    @valuser

    also the following link MAY be of some help

    Please follow the discussion into the chat – just at the time i was very slow on the uptake!

    http://wordpress.stackexchange.com/questions/75261/how-to-when-a-bp-group-created-automatically-create-a-wp-category

    c


    pandreas
    Participant

    @pandreas

    Yes, but how I will use wp_update_term function?

    I have written this:

    function example_update_category( $group_id ) {

    $group = groups_get_group( array( ‘group_id’ => $group_id ) );
    $name = $group->name;
    $groupid = $group->id;

    wp_update_term(
    189, //for a specific category item
    ‘category’,
    array(
    ‘name’ => $name
    )
    );
    }
    add_action( ‘groups_details_updated ‘, ‘example_update_category’, 1, 1 );

    But it doesn’t seem to work. What is wrong? Hook maybe?

    Thank you in advance!

    Andreas

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Function to create a Category for each Group on creation’ is closed to new replies.
Skip to toolbar