# How to add custom fields to models

**URL:** https://meta.discourse.org/t/how-to-add-custom-fields-to-models/184485
**Category:** Extras
**Tags:** pavilion
**Created:** [March 26, 2021, 7:53am UTC](https://meta.discourse.org/t/how-to-add-custom-fields-to-models/184485 "2021-03-26T07:53:44Z")
**Posts on this page:** 20
**Page:** 2

<div class="post-metadata">

### Author: ![attj](https://avatars.discourse-cdn.com/v4/letter/a/a698b9/32.png) [@attj](https://meta.discourse.org/u/attj)
#### Post date: [August 7, 2022, 9:41pm UTC](https://meta.discourse.org/t/how-to-add-custom-fields-to-models/184485/22 "2022-08-07T21:41:11Z")

</div>

Is it possible to add a custom field to tags using the same logic?

---

<div class="post-metadata">

### Author: ![AquaL1te](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/aqual1te/32/201966_2.png) [@AquaL1te](https://meta.discourse.org/u/AquaL1te)
#### Post date: [August 8, 2022, 3:22am UTC](https://meta.discourse.org/t/how-to-add-custom-fields-to-models/184485/23 "2022-08-08T03:22:38Z")

</div>

Has anyone applied this to groups? If so, can you share what you did?

I guess this would do the trick. Hopefully it’s still up-to-date.

[https://github.com/pavilionedu/discourse-group-custom-fields](https://github.com/pavilionedu/discourse-group-custom-fields)

---

<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, 7:41am UTC](https://meta.discourse.org/t/how-to-add-custom-fields-to-models/184485/24 "2022-08-08T07:41:40Z")

</div>

> [@attj](#):
>
> Is it possible to add a custom field to tags using the same logic?

No, tags don’t have custom fields. What are you trying to accomplish?

> [@AquaL1te](#):
>
> Has anyone applied this to groups? If so, can you share what you did?
> 
> I guess this would do the trick. Hopefully it’s still up-to-date.
> 
> [GitHub - pavilionedu/discourse-group-custom-fields · GitHub](https://github.com/pavilionedu/discourse-group-custom-fields)

Yes, that repository should work. Just change all instances of `my_field` to the name of your field.

---

<div class="post-metadata">

### Author: ![attj](https://avatars.discourse-cdn.com/v4/letter/a/a698b9/32.png) [@attj](https://meta.discourse.org/u/attj)
#### Post date: [August 8, 2022, 7:58am UTC](https://meta.discourse.org/t/how-to-add-custom-fields-to-models/184485/25 "2022-08-08T07:58:45Z")

</div>

> [@angus](#):
>
> No, tags don’t have custom fields. What are you trying to accomplish?

I am trying to create the following plugin: [Creating a User - Tag relation plugin](https://meta.discourse.org/t/creating-a-user-tag-relation-plugin/235208)

---

<div class="post-metadata">

### Author: ![AquaL1te](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/aqual1te/32/201966_2.png) [@AquaL1te](https://meta.discourse.org/u/AquaL1te)
#### Post date: [August 26, 2022, 1:45pm UTC](https://meta.discourse.org/t/how-to-add-custom-fields-to-models/184485/26 "2022-08-26T13:45:04Z")

</div>

Is there some more documentation about how to customize these group fields? It works indeed great with this boilerplate code. But how to extend it properly?

I for example want to add a few input fields, like:

```html
<div class="control-group">
  <label class="control-label" for="map">Chapter coordinates</label>
  <input name="chapter_coordinates" id="chapter_coordinates" class="ember-text-field ember-view input-xlarge" value={{group.custom_fields.group_coordinates}} placeholder="E.g. 52.3727598,4.8936041" type="text">
  <div class="control-instructions">Fetch coordinates from https://nominatim.openstreetmap.org/</div>
</div>

```

But I’m just guessing what to do here and then add the rest of the Discourse stuff. The example above is to use a field for coordinates. I intent to use the `g.json` to create a map based on the group metadata.

I also intend to add a checkbox for emailing the group with a mail setup for that group.

```html
<div class="control-group">
  <label class="control-label" for="map">Contact chapter by email</label>
  {{input type="checkbox" checked=group.custom_fields.contact_group_by_email}}
  <span>{{i18n 'admin.groups.contact_group_by_email.label'}}</span>
</div>

```

However, this field is best placed in the interaction tab. How to know what to do to get it there? I would like to become more proficient with this. But I get the idea that this information is just in the code, not documented somewhere. Which is probably fine, but just takes more time and effort to find what I’m looking for. Although what I want to add is quite small, just a few fields to the groups 🤓

---

<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 30, 2022, 11:56am UTC](https://meta.discourse.org/t/how-to-add-custom-fields-to-models/184485/27 "2022-08-30T11:56:21Z")

</div>

Yes, it can be tricky and [a bit overwhelming when starting to navigate the Discourse codebase](https://meta.discourse.org/t/how-to-start-building-stuff-for-discourse-if-youre-newbie-like-myself/45954). It sounds like you’re trying to work with GeoJSON. Have you considered using or extending the Locations Plugin? That’s already set up to work with GeoJSON in Discourse.

> [@Locations Plugin earth\_africa](https://meta.discourse.org/t/locations-plugin/69742):
>
> information_sourceSummary Allows you to associate geocoded locations with Topics and show them on a beautiful map. Also allows your users to record their location and collectively display them on a central users map on your site.hammer_and_wrenchRepository Link [GitHub - merefield/discourse-locations: Tools for handling locations in Discourse · GitHub](https://github.com/merefield/discourse-locations)open_bookInstall Guide [How to install plugins in Discourse](https://meta.discourse.org/t/install-plugins-in-discourse/19157)heartSponsorship Please consider becoming an ongoing [spons…](https://github.com/sponsors/merefield)

### A course in plugin development?

I’ve been considering running a free course in discourse plugin development, which is essentially what you need. I’ve already written the course materials for a course in theme development (see below). If 30 people vote for one in plugin development I’ll write a give a course (via zoom) in it.

_Poll ([view on site](https://meta.discourse.org/t/how-to-add-custom-fields-to-models/184485/27))_

> **Introduction to theme development**
>
> 1. [GitHub - pavilionedu/discourse-theme-introduction · GitHub](https://github.com/pavilionedu/discourse-theme-introduction)
> 2. [GitHub - pavilionedu/discourse-theme-css · GitHub](https://github.com/pavilionedu/discourse-theme-css)
> 3. [GitHub - pavilionedu/discourse-theme-colors · GitHub](https://github.com/pavilionedu/discourse-theme-colors)
> 4. [https://github.com/pavilionedu/discourse-theme-html-one](https://github.com/pavilionedu/discourse-theme-html-one)
> 5. [GitHub - pavilionedu/discourse-theme-html-two · GitHub](https://github.com/pavilionedu/discourse-theme-html-two)
> 6. [GitHub - pavilionedu/discourse-theme-javascript-one · GitHub](https://github.com/pavilionedu/discourse-theme-javascript-one)
> 7. [GitHub - pavilionedu/discourse-theme-javascript-two · GitHub](https://github.com/pavilionedu/discourse-theme-javascript-two)

\*ps if it hits 30 please let me know.

---

<div class="post-metadata">

### Author: ![AquaL1te](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/aqual1te/32/201966_2.png) [@AquaL1te](https://meta.discourse.org/u/AquaL1te)
#### Post date: [August 30, 2022, 3:32pm UTC](https://meta.discourse.org/t/how-to-add-custom-fields-to-models/184485/28 "2022-08-30T15:32:50Z")

</div>

Awesome! I actually will use a fork of [this Fairphone community map](https://map.fairphone.community/). They use the raw YAML output of a topic. I’ve modified my fork to use the data from g.json to draw markers on the map. I just miss a few fields such as coordinates and a boolean to use an email address in the map. Maybe some more, but first I’ll need to know how things work. Thanks for the links! Will have a look this week and see how far I’ll get. A course would be awesome too! My JS/Ruby skills are now though. Mainly Python/Bash and some crumbs from other language’s.

---

<div class="post-metadata">

### Author: ![attj](https://avatars.discourse-cdn.com/v4/letter/a/a698b9/32.png) [@attj](https://meta.discourse.org/u/attj)
#### Post date: [August 30, 2022, 4:48pm UTC](https://meta.discourse.org/t/how-to-add-custom-fields-to-models/184485/29 "2022-08-30T16:48:49Z")

</div>

Is it possible to add custom fields to posts? Ideally from the post composer.

---

<div class="post-metadata">

### Author: ![ldy](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ldy/32/109175_2.png) [@ldy](https://meta.discourse.org/u/ldy)
#### Post date: [September 13, 2022, 6:52am UTC](https://meta.discourse.org/t/how-to-add-custom-fields-to-models/184485/30 "2022-09-13T06:52:10Z")

</div>

What is the minimum discourse version required to use this plugin?  
(discourse-topic-custom-fields)

---

<div class="post-metadata">

### Author: ![FerrariFlunker](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ferrariflunker/32/258011_2.png) [@FerrariFlunker](https://meta.discourse.org/u/FerrariFlunker)
#### Post date: [October 7, 2022, 4:32am UTC](https://meta.discourse.org/t/how-to-add-custom-fields-to-models/184485/31 "2022-10-07T04:32:53Z")

</div>

> [@angus](#):
>
> Yes, that repository should work. Just change all instances of `my_field` to the name of your field.

Is this [GitHub - pavilionedu/discourse-group-custom-fields · GitHub](https://github.com/pavilionedu/discourse-group-custom-fields) still up to date?

The reason I ask is because I attempted to implement a new custom group field, but it doesn’t seem to be saving/persisting the value I enter into the input field after clicking Save.

Here’s the commit with all of the changes I did that (I think) correctly followed the edu repo’s structure: [add discord\_role\_id field to groups · aloha-pk/discourse-discord-sync@fd3eef1 · GitHub](https://github.com/aloha-pk/discourse-discord-sync/commit/fd3eef108bd31611cbbf117ce31a212515cfaf07#diff-0dbe66e3369f3c1d287bc1d992418ec4038c3362448fa427211e1ba4d3860bd8)

---

<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: [October 7, 2022, 2:44pm UTC](https://meta.discourse.org/t/how-to-add-custom-fields-to-models/184485/32 "2022-10-07T14:44:20Z")

</div>

> [@FerrariFlunker](#):
>
> Is this [GitHub - pavilionedu/discourse-group-custom-fields](https://github.com/pavilionedu/discourse-group-custom-fields) still up to date?

Yes, I just tested it and it’s working as expected.

```plaintext
{{input type="text" checked=group.custom_fields.discord_role_id}}

```

Your issue is that you’ve changed the input type to `text`, but left the value input as `checked`. You need to change `checked` to `value`.

---

<div class="post-metadata">

### Author: ![FerrariFlunker](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ferrariflunker/32/258011_2.png) [@FerrariFlunker](https://meta.discourse.org/u/FerrariFlunker)
#### Post date: [October 8, 2022, 12:55am UTC](https://meta.discourse.org/t/how-to-add-custom-fields-to-models/184485/33 "2022-10-08T00:55:25Z")

</div>

> [@angus](#):
>
> Your issue is that you’ve changed the input type to `text`, but left the value input as `checked`. You need to change `checked` to `value`.

Welp, that’ll do it 🤦‍♂️ Thanks for the help! 😄

---

<div class="post-metadata">

### Author: ![FerrariFlunker](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ferrariflunker/32/258011_2.png) [@FerrariFlunker](https://meta.discourse.org/u/FerrariFlunker)
#### Post date: [October 9, 2022, 5:19pm UTC](https://meta.discourse.org/t/how-to-add-custom-fields-to-models/184485/34 "2022-10-09T17:19:53Z")

</div>

> [@FerrariFlunker](#):
>
> Welp, that’ll do it 🤦‍♂️

Was just able to rebuild with this change, but unfortunately am still seeing the same issue. The value I entered gets cleared out after I save and re-load the page.

Any other suggestions @angus?

---

<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: [October 10, 2022, 7:23am UTC](https://meta.discourse.org/t/how-to-add-custom-fields-to-models/184485/35 "2022-10-10T07:23:40Z")

</div>

Did you change anything else from the example? The example works. Try starting with the example itself. If that’s also failing in your environment, then something else is going on. If the example works in your environment, then progressively work through the changes you made until you find the one that’s breaking it.

---

<div class="post-metadata">

### Author: ![pinder99](https://avatars.discourse-cdn.com/v4/letter/p/dfb087/32.png) [@pinder99](https://meta.discourse.org/u/pinder99)
#### Post date: [May 11, 2023, 6:54am UTC](https://meta.discourse.org/t/how-to-add-custom-fields-to-models/184485/36 "2023-05-11T06:54:30Z")

</div>

Hi @angus added a custom filed using this plugin and it is working fine now how can i show this filed on category page below category name

---

<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: [May 11, 2023, 7:42am UTC](https://meta.discourse.org/t/how-to-add-custom-fields-to-models/184485/37 "2023-05-11T07:42:41Z")

</div>

Here’s how you can figure that out:

1. Find the templates in discourse/discourse that render the category page (search for a html element on the page)

2. Find a plugin-outlet in one of those templates

3. Use the plugin outlet as described here

> [@Using Plugin Outlet Connectors from a Theme or Plugin](https://meta.discourse.org/t/using-plugin-outlet-connectors-from-a-theme-or-plugin/32727):
>
> Discourse includes hundreds of Plugin Outlets which can be used to inject new content or replace existing contend in the Discourse UI. ‘Outlet arguments’ are made available so that content can be customized based on the context. Choosing an outlet To find the name of a plugin outlet, search Discourse core for “\<PluginOutlet”, or use the [plugin outlet locations](https://meta.discourse.org/t/plugin-outlet-locations-theme-component/100673) theme component. (e.g. topic-above-posts). Wrapper outlets Some outlets in core look like \<PluginOutlet @name="foo" /\>. These allow you…

---

<div class="post-metadata">

### Author: ![pinder99](https://avatars.discourse-cdn.com/v4/letter/p/dfb087/32.png) [@pinder99](https://meta.discourse.org/u/pinder99)
#### Post date: [May 11, 2023, 8:35am UTC](https://meta.discourse.org/t/how-to-add-custom-fields-to-models/184485/38 "2023-05-11T08:35:57Z")

</div>

written this code but it is still not working can you tell the issue

```plaintext
<script type="text/discourse-plugin" version="0.8">
  const { getOwner } = require("discourse-common/lib/get-owner");
  const { htmlSafe } = require("@ember/string");

  api.decorateWidget("category-heading:after", (helper) => {
    const category = helper.attrs.category;

    if (category.custom_fields?.location) {
      const customLocation = htmlSafe(category.custom_fields.location);
      const template = getOwner(helper).lookup("template:components/category-header");

      return template.fragment.build("custom-location", { customLocation });
    }
  });
</script>

<script type="text/x-handlebars" data-template-name="components/category-header/custom-location">
  <div class="category-custom-location">{{customLocation}}</div>
</script>

```

---

<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: [May 11, 2023, 3:19pm UTC](https://meta.discourse.org/t/how-to-add-custom-fields-to-models/184485/39 "2023-05-11T15:19:31Z")

</div>

What part of it is not working? For example, what do you see when you stick a `console.log(category.custom_fields)` in there?

---

<div class="post-metadata">

### Author: ![Westcan](https://avatars.discourse-cdn.com/v4/letter/w/a9a28c/32.png) [@Westcan](https://meta.discourse.org/u/Westcan)
#### Post date: [March 10, 2024, 3:09am UTC](https://meta.discourse.org/t/how-to-add-custom-fields-to-models/184485/40 "2024-03-10T03:09:21Z")

</div>

I installed this plugin today with this code and it appears to be broken: [GitHub - pavilionedu/discourse-category-custom-fields: Discourse plugin showing how to add custom fields to Discourse categories · GitHub](https://github.com/pavilionedu/discourse-category-custom-fields.git)

It installs as “Education Category Custom Field” and the author information isn’t there:

 ![image](https://global.discourse-cdn.com/meta/original/4X/2/3/4/234fd9466e6d9df206a01e607a1810852927c215.png)

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [March 10, 2024, 4:09am UTC](https://meta.discourse.org/t/how-to-add-custom-fields-to-models/184485/41 "2024-03-10T04:09:20Z")

</div>

What do you expect to happen? It’s not supposed to do anything without modification.

[Previous page](https://meta.discourse.org/t/how-to-add-custom-fields-to-models/184485.md?page=1)

[Next page](https://meta.discourse.org/t/how-to-add-custom-fields-to-models/184485.md?page=3)
