# Nieuwe tags toewijzen aan een taggroep

**URL:** https://meta.discourse.org/t/assign-new-tags-to-a-tag-group/369605
**Category:** Development
**Tags:** tags, tag-groups
**Created:** [10 juni 2025 om 19:27 UTC](https://meta.discourse.org/t/assign-new-tags-to-a-tag-group/369605 "2025-06-10T19:27:09Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![tknospdr](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tknospdr/32/529762_2.png) [@tknospdr](https://meta.discourse.org/u/tknospdr)
#### Post date: [10 juni 2025 om 19:27 UTC](https://meta.discourse.org/t/assign-new-tags-to-a-tag-group/369605/1 "2025-06-10T19:27:09Z")

</div>

Trying to figure out a way to have tags created by users in a specific category be automatically assigned to a specified tag group. I searched and didn’t find anything currently available.

Does this code look like it would do what I want?

```plaintext
enabled_site_setting :auto_tag_to_group_enabled

# Plugin settings
register_site_setting :auto_tag_to_group_enabled, default: true
register_site_setting :auto_tag_category_name, default: "RestrictedCategory"
register_site_setting :auto_tag_group_name, default: "RestrictedTags"

after_initialize do
  module ::DiscourseAutoTagToGroup
    class Engine < ::Rails::Engine
      engine_name "discourse_auto_tag_to_group"
      isolate_namespace DiscourseAutoTagToGroup
    end
  end

  require_dependency "topic"
  class ::Topic
    after_save :auto_assign_new_tags_to_group, if: :auto_tag_to_group_conditions_met?

    private

    def auto_tag_to_group_conditions_met?
      # Check if plugin is enabled and topic has tags
      SiteSetting.auto_tag_to_group_enabled && self.tags.present?
    end

    def auto_assign_new_tags_to_group
      # Get the configured category and tag group names
      target_category_name = SiteSetting.auto_tag_category_name
      target_tag_group_name = SiteSetting.auto_tag_group_name

      # Find the category and tag group
      target_category = Category.find_by(name: target_category_name)
      target_tag_group = TagGroup.find_by(name: target_tag_group_name)

      # Exit if category or tag group not found
      return unless target_category && target_tag_group
      return unless self.category_id == target_category.id

      # Get the timestamp of the topic creation
      topic_created_at = self.created_at

      self.tags.each do |tag|
        # Check if the tag was created close to the topic creation time
        # Allow a small window (e.g., 1 minute) to account for processing delays
        if tag.created_at && tag.created_at >= (topic_created_at - 1.minute)
          # Check if tag is already in the target tag group
          unless TagGroupMembership.exists?(tag_id: tag.id, tag_group_id: target_tag_group.id)
            # Add the tag to the tag group
            TagGroupMembership.create!(tag_id: tag.id, tag_group_id: target_tag_group.id)
          end
        end
      end
    end
  end
end

```

---

<div class="post-metadata">

### Author: ![faithererer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/faithererer/32/512392_2.png) [@faithererer](https://meta.discourse.org/u/faithererer)
#### Post date: [14 juli 2025 om 06:19 UTC](https://meta.discourse.org/t/assign-new-tags-to-a-tag-group/369605/2 "2025-07-14T06:19:02Z")

</div>

Ik ben ook een soortgelijke vereiste tegengekomen. Heb je het opgelost?

---

<div class="post-metadata">

### Author: ![tknospdr](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tknospdr/32/529762_2.png) [@tknospdr](https://meta.discourse.org/u/tknospdr)
#### Post date: [14 juli 2025 om 13:07 UTC](https://meta.discourse.org/t/assign-new-tags-to-a-tag-group/369605/3 "2025-07-14T13:07:39Z")

</div>

Nee. Laat het me weten als je iets bedenkt.
