Creating and configuring custom user fields

I think you could reorder them with the rails console. I chose to enter the fields with their IDs because it’s more difficult to type a name without typos

ids_in_order = [3, 7, 8, 5, 1, 2]

ids_in_order.each_with_index do |id, index|
  UserField.find(id).update!(position: index)
end
1 Like

Here is what the “reorder categories” window looks like. I’m guessing if this exists in Discourse there could be a way to use it for user fields too?

1 Like

I don’t believe so, but I recently wrote a theme component that hides the original display of custom fields on the user card and profile pages and then renders them again, optionally making some HTML-safe (they are adding some info via the API to a custom field that users can’t edit). I think the same idea could be used to add an order field to the custom fields to change the order they are displayed on the card and/or profile.

I think you can use the existing methods for reordering the user fields. It is a little impractical that you have to open the menu again after every position change. This is somewhat annoying when moving across several positions.

But a component that would make the buttons always visible or a checkbox for a “reorder mode” at the top that makes them visible outside the menu could already improve the experience.

1 Like

Oh. Funny. Yeah. Oops. That’s tons better than my idea. :rofl:

I have a custom field and I have set it as searchable. However on the user page /u the only search field says “filter by username”. When I search for the contents of the custom field, no results show.

I can see the values in the list (I turned the column on in the customizer) So I can see that those values clearly exist.

What am I doing wrong?

Strangely, the values of the custom field are links, e.g. u?cards=no&name=Female&order=likes_received and when clicked it runs a search for the value in the name field. And it returns nothing.

These are the prefs for the field:

Also (I don’t know if this is a clue) when I sort by that custom field column, whether it is forward or backward, there are always users with empty fields at the top of the list (different users) . Only a small number of users have content in this field, but I would have expected all the empties to be at the top or the bottom of the list, not both.

Creating a Discourse multiselect user field with many options

Entering dozens or hundreds of choices one at a time in Discourse’s custom user-field editor is tedious. I’ve been working on a multipurpose CLI tool for Discourse called dsc, which makes operations like this easier and more deterministic using local files. Just one of many things it can do is manipulate Custom User Fields.

dsc can create a new custom user field from a YAML file, including the entire ordered option list in one operation. The ucf command is an alias for user-field.

First, list your existing custom user fields:

dsc ucf list myforum --format yaml

This shows their IDs, types, options and settings.

Prepare the field

Create user-fields.yaml, replacing the source URL, question and options with your own. Make sure your YAML is valid - it’s easy to get wrong.

For example, here’s a country multiselect with 25 options:

version: 1
source: https://forum.example.org
user_fields:
  - name: Countries
    description: Which countries do you work in? Select all that apply.
    field_type: multiselect
    editable: true
    requirement: on_signup
    show_on_profile: false
    show_on_signup: true
    show_on_user_card: false
    searchable: false
    options:
      - Argentina
      - Australia
      - Brazil
      - Canada
      - China
      - Denmark
      - Egypt
      - France
      - Germany
      - India
      - Ireland
      - Italy
      - Japan
      - Kenya
      - Mexico
      - Netherlands
      - New Zealand
      - Nigeria
      - Norway
      - Singapore
      - South Africa
      - Spain
      - Sweden
      - United Kingdom
      - United States

This creates one field with 25 options, rather than 25 separate fields. You can extend the list as needed - I’ve used this approach successfully for a field with 250+ options.

Preview, create and verify

dsc ucf push --dry-run myforum user-fields.yaml
# Review the plan and the complete YAML file before applying.
dsc ucf push myforum user-fields.yaml
dsc ucf show myforum <field-id> --format yaml

Happy to get feedback on dsc including ideas for missing features. Create issues on the GH repo please.

2 Likes