# Getting user profile custom field via JS

**URL:** https://meta.discourse.org/t/getting-user-profile-custom-field-via-js/32257
**Category:** Feature
**Created:** [18 أغسطس 2015، 5:08م UTC](https://meta.discourse.org/t/getting-user-profile-custom-field-via-js/32257 "2015-08-18T17:08:54Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![dleung](https://avatars.discourse-cdn.com/v4/letter/d/a183cd/32.png) [@dleung](https://meta.discourse.org/u/dleung)
#### Post date: [18 أغسطس 2015، 5:08م UTC](https://meta.discourse.org/t/getting-user-profile-custom-field-via-js/32257/1 "2015-08-18T17:08:54Z")

</div>

Hi,

I’m wondering if it is possible to retrieve user profile custom fields in our customized header using something like this:

```javascript
$.ajax("/session/current").then(function (json) { 
    console.log(json.custom.user_field_4);
}

```

What would be the correct syntax?  
Thanks!

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [18 أغسطس 2015، 5:13م UTC](https://meta.discourse.org/t/getting-user-profile-custom-field-via-js/32257/2 "2015-08-18T17:13:44Z")

</div>

You want something like

```javascript
$.ajax("/session/current").then(function(result) {
  console.log(result.current_user.custom_fields);
});

```

---

<div class="post-metadata">

### Author: ![dleung](https://avatars.discourse-cdn.com/v4/letter/d/a183cd/32.png) [@dleung](https://meta.discourse.org/u/dleung)
#### Post date: [18 أغسطس 2015، 5:21م UTC](https://meta.discourse.org/t/getting-user-profile-custom-field-via-js/32257/3 "2015-08-18T17:21:38Z")

</div>

Thanks, how do I get to the attribute?  
console.log(result.current\_user.custom\_fields); yields an object,  
i also tried:  
result.current\_user.custom\_fields.my\_field\_name  
and  
result.current\_user.custom\_fields.user\_field\_4  
they all return “undefined”

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [18 أغسطس 2015، 5:29م UTC](https://meta.discourse.org/t/getting-user-profile-custom-field-via-js/32257/4 "2015-08-18T17:29:43Z")

</div>

You will need to whitelist the custom fields you want using the “`public_user_custom_fields`” site setting.

---

<div class="post-metadata">

### Author: ![dleung](https://avatars.discourse-cdn.com/v4/letter/d/a183cd/32.png) [@dleung](https://meta.discourse.org/u/dleung)
#### Post date: [18 أغسطس 2015، 5:32م UTC](https://meta.discourse.org/t/getting-user-profile-custom-field-via-js/32257/5 "2015-08-18T17:32:01Z")

</div>

got it thanks! 😄

---

<div class="post-metadata">

### Author: ![Nuck](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nuck/32/121732_2.png) [@Nuck](https://meta.discourse.org/u/Nuck)
#### Post date: [7 أبريل 2016، 1:44ص UTC](https://meta.discourse.org/t/getting-user-profile-custom-field-via-js/32257/6 "2016-04-07T01:44:44Z")

</div>

Is this still accurate? I’ve added `pro_expires_at` as a custom field on my user (`User.find('nuck').tap { |u| u.custom_fields['pro_expires_at'] = 5.weeks.from_now.to_i }.save`) and stuffed that into `public_user_custom_fields` in the site settings panel, but I’m just not seeing it in the preload data?

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [8 أبريل 2016، 4:44م UTC](https://meta.discourse.org/t/getting-user-profile-custom-field-via-js/32257/7 "2016-04-08T16:44:26Z")

</div>

It definitely should still be working. We haven’t removed it and some plugins rely on it to work.

What preload info are you looking at? Viewing a user’s profile?

---

<div class="post-metadata">

### Author: ![Nuck](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nuck/32/121732_2.png) [@Nuck](https://meta.discourse.org/u/Nuck)
#### Post date: [8 أبريل 2016، 9:40م UTC](https://meta.discourse.org/t/getting-user-profile-custom-field-via-js/32257/8 "2016-04-08T21:40:38Z")

</div>

> [@eviltrout](#):
>
> What preload info are you looking at? Viewing a user’s profile?

`currentUser` and the `custom_user_fields` attribute that the PostSerializer says should be present on serialized `Post` objects

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [11 أبريل 2016، 7:21م UTC](https://meta.discourse.org/t/getting-user-profile-custom-field-via-js/32257/9 "2016-04-11T19:21:39Z")

</div>

It should definitely work if it’s `public_user_custom_fields` – I know we’re using this feature on some plugins and the data is indeed being displayed properly. Maybe try restarting your dev server? Does it appear in the JSON request?

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [11 أبريل 2016، 7:33م UTC](https://meta.discourse.org/t/getting-user-profile-custom-field-via-js/32257/10 "2016-04-11T19:33:49Z")

</div>

If you want a custom\_user\_field on the post object you can do this:

> <https://github.com/xfalcox/discourse-signatures/blob/1ff4e7e1d57eed825febdbd58980a73bef03cb6b/plugin.rb#L19>

---

<div class="post-metadata">

### Author: ![Nuck](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nuck/32/121732_2.png) [@Nuck](https://meta.discourse.org/u/Nuck)
#### Post date: [14 أبريل 2016، 2:32ص UTC](https://meta.discourse.org/t/getting-user-profile-custom-field-via-js/32257/11 "2016-04-14T02:32:13Z")

</div>

> [@eviltrout](#):
>
> It should definitely work if it’s public\_user\_custom\_fields – I know we’re using this feature on some plugins and the data is indeed being displayed properly

`public_user_custom_fields` in the admin panel, right? Not some setting elsewhere?

> [@eviltrout](#):
>
> Maybe try restarting your dev server? Does it appear in the JSON request?

I was actually developing against our staging server which rebuilds the docker every time so I’m certain it was restarted (and then some). I’ll look at the JSON request this weekend and see if I can pin this down.

> [@Falco](#):
>
> If you want a custom\_user\_field on the post object you can do this:

Wouldn’t this cause an N+1 query? Is there any solid documentation on the whole custom field system somewhere? I didn’t realize you could register the custom field type or append to `serialized_current_user_fields`

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [14 أبريل 2016، 3:13ص UTC](https://meta.discourse.org/t/getting-user-profile-custom-field-via-js/32257/12 "2016-04-14T03:13:04Z")

</div>

> [@Nuck](#):
>
> Wouldn’t this cause an N+1 query?

I don’t think so, because the serializer already has the user object, is just a matter of including it in the payload. I can be wrong, tough.

> [@Nuck](#):
>
> Is there any solid documentation on the whole custom field system somewhere?

Nope, but there are plenty of opensource plugins and the Discourse source code itself.

> [@Nuck](#):
>
> I didn’t realize you could register the custom field type or append to serialized\_current\_user\_fields

I found it trough other plugins code too.

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [14 أبريل 2016، 3:05م UTC](https://meta.discourse.org/t/getting-user-profile-custom-field-via-js/32257/13 "2016-04-14T15:05:26Z")

</div>

Right, `public_user_custom_fields` in the admin panel. Those fields should end up in the JSON request. I’d double check there. Without a development environment your life is going to be a lot harder, as the next step would be to go into the code and add `Rails.logger.info` to try and figure out why those fields are not being passed through when they should.

---

<div class="post-metadata">

### Author: ![jimkleiber](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jimkleiber/32/121814_2.png) [@jimkleiber](https://meta.discourse.org/u/jimkleiber)
#### Post date: [16 مارس 2023، 12:22ص UTC](https://meta.discourse.org/t/getting-user-profile-custom-field-via-js/32257/14 "2023-03-16T00:22:07Z")

</div>

لسبب ما، لا يمكنني رؤية حقول المستخدم المخصصة في وحدة التحكم التي أضفتها في قسم المسؤول \> تخصيص \> حقول المستخدم.

لقد وضعت ما يلي في إضافة:

```plaintext
<script type="text/discourse-plugin" version="0.8.42">
   const user = api.getCurrentUser();
   console.log(user);
</script>

```

عندما أنظر إلى كائن المستخدم، أرى كائنًا فارغًا تحت إدخال custom\_fields. عندما أغيره إلى `console.log(user.custom_fields);` لا يزال يظهر كائنًا فارغًا.

المستخدم الحالي لديه ثلاثة حقول مخصصة مملوءة، لقد أضفت مفاتيح هذه الحقول الثلاثة، بتنسيق `test_topic` إلى `public user custom fields` و `staff user custom fields` ومع ذلك لا يزال الكائن يبدو فارغًا في وحدة التحكم.

لقد سألت ChatGPT (lol) ويبدو أنها لا تساعدني.

أي اقتراحات حول ما يمكن أن يحدث؟

تحديث: أعتقد أن إحدى المشاكل كانت أنني كنت أستخدم `custom_fields` والآن يبدو أنها تحت تسمية `user_fields`، كما هو موضح هنا:

> [@How do I access Custom Fields in a theme component?](https://meta.discourse.org/t/how-do-i-access-custom-fields-in-a-theme-component/134701/2):
>
> You could try something like this: \<script type="text/discourse-plugin" version="0.8.27"\> let currentUser = api.getCurrentUser(); if (currentUser) { api.container.lookup('store:main').find('user', currentUser.username).then((user) =\> { console.log('user', user.user\_fields); }); } \</script\> See [Using the PluginAPI in Site Customizations](https://meta.discourse.org/t/using-the-pluginapi-in-site-customizations/41281) for details about the "text/discourse-plugin" script type.

ومع ذلك، يبدو أنه يعرض لي فقط قيمة حقل المستخدم المخصص ورقمًا للمفتاح، بدلاً من المفتاح المخصص الذي أدخلته في المسؤول \> تخصيص \> حقول المستخدم.

---

<div class="post-metadata">

### Author: ![Heliosurge](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/heliosurge/32/571810_2.png) [@Heliosurge](https://meta.discourse.org/u/Heliosurge)
#### Post date: [16 مارس 2023، 12:49ص UTC](https://meta.discourse.org/t/getting-user-profile-custom-field-via-js/32257/15 "2023-03-16T00:49:32Z")

</div>

قد يكون مكون السمة هذا محل اهتمام اعتمادًا على ما تتطلع إلى القيام به بحقل المستخدم.

> [@Sidebar User Field Toggle](https://meta.discourse.org/t/sidebar-user-field-toggle/257634):
>
> information_sourceSummary Add toggles into new sidebarhammer_and_wrenchRepository [GitHub - Lhcfl/sidebar-user-field-toggle: Add toggles into new sidebar](https://github.com/Lhcfl/sidebar-user-field-toggle)questionInstall Guide [How to install a theme or theme component](https://meta.discourse.org/t/how-do-i-install-a-theme-or-theme-component/63682)open_bookNew to Discourse Themes? [Beginner’s guide to using Discourse Themes](https://meta.discourse.org/t/beginners-guide-to-using-discourse-themes/91966) Install this theme component Special thanks to @Kinetiksoft on Discourse Meta for supporting me in developing this theme component. Without @Kinetiksoft, this theme co…

يضيف هذا إخفاء المنشورات بنوع من الكلمات التي يراقبها المستخدم.

> [@Allow users to block keywords](https://meta.discourse.org/t/allow-users-to-block-keywords/256228):
>
> information_sourceSummary This theme component allows users to block keywords. Requires three custom user fields to be created in Admin settings in order to function.eyeglassesPreview I can’t preview this on theme-creator because it requires creating user fields, but I’ve included screenshots below.hammer_and_wrenchRepository [Github - a-very-old-sock/discourse-keyword-block](https://github.com/a-very-old-sock/discourse-keyword-block)questionInstall Guide [How to install a theme or theme component](https://meta.discourse.org/t/how-do-i-install-a-theme-or-theme-component/63682)open_bookNew to Discou…

قد يوفر عرض المصدر أفكارًا.

---

<div class="post-metadata">

### Author: ![jimkleiber](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jimkleiber/32/121814_2.png) [@jimkleiber](https://meta.discourse.org/u/jimkleiber)
#### Post date: [16 مارس 2023، 12:57ص UTC](https://meta.discourse.org/t/getting-user-profile-custom-field-via-js/32257/16 "2023-03-16T00:57:03Z")

</div>

أحاول بشكل أساسي إنشاء مكون سمة صغير يضمن أن الحقول المخصصة للمستخدم التي أضيفها في المسؤول \> تخصيص \> حقول المستخدم هي:

1. تمت إضافتها إلى i18n قابلة للترجمة حتى أتمكن من تخصيص نصها في المسؤول \> تخصيص \> قسم النص
2. تمت إضافتها كمفاتيح استيفاء حتى أتمكن من استخدامها كـ `%{key}` في المسؤول \> تخصيص \> قسم النص

بشكل أساسي أحاول حل مشكلتي التي نشرتها هنا:  
[https://meta.discourse.org/t/create-and-configure-custom-user-fields/113192/55](https://meta.discourse.org/t/create-and-configure-custom-user-fields/113192/55)

لكنني لم أتمكن من تجاوز الجزء الأول المتمثل في الحصول على تركيبة المفتاح/القيمة من حقول المستخدم في كائن المستخدم.

سألقي نظرة على ما أضفته، شكرًا لك!

---

<div class="post-metadata">

### Author: ![Heliosurge](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/heliosurge/32/571810_2.png) [@Heliosurge](https://meta.discourse.org/u/Heliosurge)
#### Post date: [16 مارس 2023، 1:12ص UTC](https://meta.discourse.org/t/getting-user-profile-custom-field-via-js/32257/17 "2023-03-16T01:12:16Z")

</div>

ما زلت جديدًا جدًا وأتعلم الأشياء أثناء تقدمي. لا يمكنني التأكيد، ولكن إذا كانت هناك ترجمات لغوية، فهناك مكونان إضافيان يقومان بالترجمة.

> [@Discourse Translator](https://meta.discourse.org/t/discourse-translator/32630):
>
> discourse2Summary Discourse Translator translates posts on Discourse using Microsoft, Google, or Yandex translation APIs.hammer_and_wrenchRepository Link [https://github.com/discourse/discourse-translator](https://github.com/discourse/discourse-translator)open_bookInstall Guide [How to install plugins in Discourse](https://meta.discourse.org/t/install-plugins-in-discourse/19157)Features Each post is only translated once per locale which saves you moneybagNote that language detection is also run once for every post. Be careful when enabli…

و

> [@Multilingual Plugin globe\_with\_meridians](https://meta.discourse.org/t/multilingual-plugin/142740):
>
> The Multilingual Plugin makes it possible for administrators of a multilingual forum to create a rich and comprehensible environment for all users and guests, even if they don’t speak the default or majority language of the forum. [page_facing_up Get the code](https://github.com/paviliondev/discourse-multilingual)[book Read the documentation](https://coop.pavilion.tech/docs?ascending=false&category=94&solved=false)Overview of features The Multilingual Plugin extends the existing Discourse locale system to give you: an administrative interface for all languages used on your forum; a [content language system](https://coop.pavilion.tech/docs?topic=1693) whic…

أعتذر إذا أسأت فهم ما تعمل على تحقيقه إذا كان الأمر غير صحيح.
