Custom Fields

You can use the Custom Wizard Plugin to manage Discourse’s custom fields. The primary use case for this feature is managing custom fields for topics, posts, categories and groups created in wizard actions, but you could use it to manage custom fields for any purpose in Discourse.

https://www.loom.com/share/5175c4fa58b9461abad3e3eabe16d787

What are “Custom Fields”?

In Discourse a “custom field” is a data store associated with a standard Discourse data model. For each of the Discourse data models there is an associated “custom_fields” table in the database where you can store data associated with a particular “row” (e.g. a particular topic or post) in the standard table. The custom field data is stored according to the “type” that is set for it.

Discourse models with custom fields

  1. Users
  2. Topics
  3. Posts
  4. Categories
  5. Groups

Custom field types

  1. String
  2. Boolean
  3. Integer
  4. JSON

Custom Field Management

You can manage custom fields for all Discourse models, aside from user custom fields (see below), in the Custom Wizard Custom Field Admin UI.

Screenshot 2023-05-14 20.38.46

Screenshot 2023-05-14 20.39.01

All inputs you see above are required to register a field. We’ll go through each in turn

Class

This is the Discourse model you want to associate your custom field with (see above)

Type

This is the data type of the field (see above)

Name

This is the name of your custom field. For computational purposes it will be transformed to a lowercase underscore separated string, for example “Peanut Butter” will become “peanut_butter”.

Serializers

By default, any custom field you add to a Discourse model won’t be sent from the server to the client. Each custom field has to be explicitly “serialized” to the client in each context it is needed. The list of available serializers will change depending on what class you select.

The serializer names related to the names of the serializers in the code:

  1. Topic View: This is the data sent when you’re looking at a topic

  2. Topic List Item: This is the data sent with each list item on any topic list in Discourse.

  3. Post: The data sent for every post

  4. Category: This translates to the “BasicCategorySerializer” in the code.

User Custom Fields

You can manage user custom fields in the Customize > User Fields Admin UI. Any user fields you register there can be used in a Custom Wizard.

Custom Fields in Wizard Actions

You can add custom fields to any Topic, Post, Group or Category you create in a wizard action. For each relevant action you’ll see a “Custom” entry in the Actions section.

In the “map” entry for the custom field map you can change the selection type to “custom field” and you will see a list of the custom fields you have registered in the Custom Wizard’s Custom Field Admin UI.