创建和配置自定义用户字段

:bookmark: This guide explains how to create and configure custom user fields in Discourse, including how to add them to the signup form, user profiles, and user directory.

:person_raising_hand: Required user level: Administrator

Custom user fields allow you to collect additional information from your users beyond the standard profile fields. These fields can be displayed on user cards, user summary pages, and even retrieved using the Data Explorer plugin. This guide will walk you through the process of creating and configuring custom user fields.

Adding a user field

  1. Go to Admin > Community > User Fields (discourse.example.com/admin/config/user-fields).

  2. If you haven’t created any user fields yet, you’ll see this screen:

  3. Click the “Add user field” button to add a new field.

Choosing a field type

The field type determines the input field users will see on the signup form. Choose based on the kind of information you’re collecting:

Text field

Confirmation

Dropdown

To add options for a dropdown field:

  1. Click on the option input
  2. Type an answer and click “Create”
  3. Repeat for additional options

The completed options should look similar to this:

Setting the field name and description

  • Field Name: Appears before the input on the signup form and user profile
  • Field Description: Appears after the input to help users complete the field

:information_source: Both the field name and description are required to save a field.

Here’s how custom fields appear on the signup form:

Configuring user field options

Field Requirements

  • Optional - Optional fields may be left empty by users
  • For all users - When a field is required by all users, every account, including logged on users will be forced to fill it. This is very useful for cases such as a terms-of-service (ToS) requirement.
  • On signup - All new account will be required to fill the field.

Additionally, at the bottom of the creation form, you’ll find these options:

  • Editable after signup: Allows users to update the field from their profile page
  • Required at signup: Makes the field mandatory during account creation
  • Show on public profile: Displays the field value on the user’s summary page
  • Show on user card: Shows the field value on the user card
  • Searchable: Enables searching for users based on this field’s value in the user directory

Show on public profile

When enabled, the field value will be shown on the user’s profile page:

Show on user card

When enabled, the field value will be displayed on the user card:

Searchable

When enabled, you can search for users based on their custom field values:

Saving and editing fields

  1. Click “Save” to add the field to your site’s list of user fields
  2. To edit a field, click the “Edit” button next to it in the list
  3. To delete a field, click the “Delete” button

Adding custom fields to the user directory

  1. Go to the user directory
  2. Click the wrench icon:

  1. Check the custom user fields you want to display
  2. Click “Save”

The selected custom fields will now appear in the user directory table:

Last edited by @fzngagan 2025-07-17T09:12:38Z

Last checked by @hugh 2024-08-27T04:45:20Z

Check documentPerform check on document:
38 个赞

我认为以下标题下缺少两张图片:

不确定这是否与 Meta.discourse.org 缺少图片 相关——我也会在那里发帖。

我看得到那些图片吗?(匿名窗口中也是)

1 个赞

我看不到那些

但第二个在我的引用中有效。

编辑

1 个赞

有趣的是,我可以在引文中看到第二张图片,但在原始帖子中看不到^^

1 个赞

嗯。这很有趣。我认为 Missing images at Meta.discourse.org 问题有一个修复程序即将推出,所以希望它能得到解决。:crossed_fingers:

2 个赞

是否需要修改设置来指定自定义用户字段的最大长度?目前,在我创建的这个“Test”字段(作为测试用户字段)中,我无法在我的用户配置文件中输入任何字符(甚至无法输入“Test”),如下图所示。

我想要一个字段类型:链接/URL,可以吗?

由于 URL 是文本,因此 text 字段在技术上是可行的,@Vaping_Community。但是,您可能正在寻找其他详细信息,例如值验证或类似功能。

您可以搜索或创建一个您心中所想的 Feature 主题。:slight_smile:

3 个赞

是否有计划允许自定义用户字段使用多行/组合框?:folded_hands:

请注意,如果您想在自定义用户字段中嵌入链接,则需要使用 HTML 语法!
<a>链接文本</a>

例如,要确认社区准则/政策:

1 个赞

我能否将 Auth0 SSO 中的自定义声明关联到自定义字段?目前,用户在 Auth0 中输入字段信息,然后在注册时必须再次输入。如果可能,我想将该值映射过来。

有可能,是的:

在您的 SSO 端点中,您有能力将声明映射到有效负载,例如:

const ssoPayload = {
  nonce: nonce,
  email: user.email,
  external_id: user.sub,
  username: user.nickname,
  name: user.name,
  add_groups: a_custom_group,
  'custom.user_field_1': user['https://yourdomain.com/company_id'],
  'custom.user_field_2':  [etc...]
2 个赞

感谢 @dax

有没有办法在数据库中验证字段名?例如,我们有一个名字字段,我尝试了 custom.firstnamecustom.first_namecustom.firstName,但这些字段在注册屏幕上都没有被填充。

我已经检查了错误日志,以确认令牌字段如上所示。

深入研究一下这里的代码,这是否仅适用于 Discourse Connect SSO?我们正在使用 Auth0 插件。

语法应该是 custom.user_field_x,其中 x 是在 /admin/config/user-fields/{x}/edit 中显示的数字字段 ID。

Auth0 插件本身不提供此映射功能。

不过,仍有其他方法可以实现您所描述的功能:

  • 创建主题组件。您可以添加一个小型前端脚本,自动将 Discourse 自定义用户字段与 Auth0 中已存储的值同步。例如,当用户登录且字段为空时,脚本可以调用一个安全的端点(一个小型云函数),从 Auth0 获取字段值,并通过 API 更新 Discourse 配置文件。
  • 使用自动化工具。您还可以使用 Zapier 或 Make 等外部自动化服务,在 Discourse 之外执行同步。优点是您不必编写/维护代码,只需为第三方服务付费。
  • 定制开发。我们可以扩展 Auth0 插件本身,使其在登录时原生支持将自定义声明映射到用户字段,或者构建一个与 Auth0 插件配合使用的自定义插件。

主题组件方法的明显缺点是您需要自己编写和维护自定义代码,同时还要注意安全问题,避免引入潜在的错误或漏洞。老实说,我不会推荐这种解决方案用于您这样的生产环境。

如果我处于您的位置,我会更倾向于第二种选择,即使用第三方工具,或者考虑提交功能请求或定制工作请求(取决于我们项目经理的评估),以增强 Auth0 插件本身。

如果您有兴趣探索最后一种选择,我们可以私下继续讨论。

1 个赞