# Creating a User - Tag relation plugin

**URL:** https://meta.discourse.org/t/creating-a-user-tag-relation-plugin/235208
**Category:** Development
**Created:** [August 5, 2022, 10:02am UTC](https://meta.discourse.org/t/creating-a-user-tag-relation-plugin/235208 "2022-08-05T10:02:14Z")
**Posts on this page:** 1
**Showing post:** 7

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [August 8, 2022, 9:38am UTC](https://meta.discourse.org/t/creating-a-user-tag-relation-plugin/235208/7 "2022-08-08T09:38:51Z")

</div>

You can do this with a simple plugin.

### What you need to do

The plugin will need to do the following:

1. Add a user custom field called `known_tags`, a list of strings.

2. Add an interface in the user profile where the user can edit `known_tags`. I don’t think it’s necessary to add an entirely new profile tab for this, but you could if you wanted to. If you don’t want users editing it themselves, just make it only editable by admins and just go into users profiles and update it based on your CSV file.

3. Add an event hook which uses the `before_create_post` or `post_created` events in the `PostCreator` to add the content you want to the post based on the tags in the topic.

### How to do it

Parts 1 and 2 are very similar to the example plugins for other models in the topic linked below. See if you can figure it out by analogy. If you get really stuck ask me for a pointer there.

> [@How to add custom fields to models](https://meta.discourse.org/t/how-to-add-custom-fields-to-models/184485):
>
> This is a collection of education plugins that demonstrate how to add a custom field to different models in Discourse. They are intended as learning tools for those looking to learn how to build discourse plugins. [[GitHub-Mark] How to add a custom field to a topic](https://github.com/pavilionedu/discourse-topic-custom-fields)[[GitHub-Mark] How to add a custom field to a category](https://github.com/pavilionedu/discourse-category-custom-fields)Who they’re for These plugins are for people looking to learn more about creating Discourse plugins. Before you start working with these plugins, you should complete the [beginner…](https://meta.discourse.org/t/beginners-guide-to-creating-discourse-plugins/30515)

Part 3 will also go in your plugin.rb file. It’ll look something like this

```plaintext
on(:post_created) |post, opts, user|
  if post.is_first_post? && post.topic.tags.present?
     user_ids = UserCustomField.where(name: 'known_tags', value: post.topic.tags).pluck(:user_id)
     usernames = User.where(id: user_ids).pluck(:username)
     new_raw = post.raw + "something something #{usernames}"
     PostRevisor.new(post).revise!(
      user,
      {
        raw: new_raw,
        edit_reason: "some reason"
      },
      skip_validations: true,
      bypass_bump: true
    )
  end 
end

```

Give it a royal go yourself. If you get really stuck I’ll help. I’m always more inclined to help if there’s strong evidence you’re trying to figure it out yourself 🙂

---

_[View the full topic](https://meta.discourse.org/t/creating-a-user-tag-relation-plugin/235208)._
