# Map custom User Fields

**URL:** https://meta.discourse.org/t/map-custom-user-fields/156164
**Category:** WordPress
**Created:** [June 28, 2020, 11:44am UTC](https://meta.discourse.org/t/map-custom-user-fields/156164 "2020-06-28T11:44:03Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![tiagofreitas87](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tiagofreitas87/32/174637_2.png) [@tiagofreitas87](https://meta.discourse.org/u/tiagofreitas87)
#### Post date: [June 28, 2020, 11:44am UTC](https://meta.discourse.org/t/map-custom-user-fields/156164/1 "2020-06-28T11:44:03Z")

</div>

Hi, is there a way to map User Fields in Discourse to Wordpress user metadata?

I have a registration form in wordpress that saves metadata, I would like that to be copied to Discourse when it creates the user, to be shown in the profile, and ideally to show a pre-filled composer for the Introductions category.

Thanks

---

<div class="post-metadata">

### Author: ![tiagofreitas87](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tiagofreitas87/32/174637_2.png) [@tiagofreitas87](https://meta.discourse.org/u/tiagofreitas87)
#### Post date: [June 28, 2020, 11:55am UTC](https://meta.discourse.org/t/map-custom-user-fields/156164/2 "2020-06-28T11:55:02Z")

</div>

I think I found how to it, but needs to be changed in [wp discourse](https://github.com/discourse/wp-discourse) plugin after every update:

[https://meta.discourse.org/t/sso-settings-integration-between-wordpress-and-discourse-using-wp-plugin/49343/5](https://meta.discourse.org/t/sso-settings-integration-between-wordpress-and-discourse-using-wp-plugin/49343/5)

It would be great if @simon could implement this in the plugin settings, basically just have a way to map custom field names from WP to Discourse.

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [June 29, 2020, 6:33pm UTC](https://meta.discourse.org/t/map-custom-user-fields/156164/3 "2020-06-29T18:33:41Z")

</div>

> [@tiagofreitas87](#):
>
> I think I found how to it, but needs to be changed in [wp discourse](https://github.com/discourse/wp-discourse) plugin after every update:

I wouldn’t make any direct changes to the plugin’s code unless you are certain about what you are doing. The [WP Discourse](https://github.com/discourse/wp-discourse) plugin is built to allow it to be extended by a plugin or a theme by using [action hooks](https://developer.wordpress.org/plugins/hooks/actions/). This allows you to customize the plugin without making changes that will be overridden when you update the plugin.

To add data to the SSO payload that is sent to Discourse, use the `wpdc_sso_params' filter. Functions that hook into that filter are passed two parameters: `$params`(an array of SSO parameters that are passed to Discourse) and `$user` (the WordPress user object.)

There is an example of how to use the filter in this post:

> [@Bring over permission level from WordPress MemberPress](https://meta.discourse.org/t/bring-over-permission-level-from-wordpress-memberpress/78074/2):
>
> There are a couple of ways to do it. The easiest way is to add the Discourse group names to the SSO payload. You can do this by hooking into the wpdc\_sso\_params filter. Setting it up this way will add and remove users from groups when they login to Discourse. You’ll need to figure out the condition to check the user against to know which groups to add or remove them from. Memberpress fires an action when a membership level is added or removed. You could hook into that action and add some user me…

Let me know if you have any trouble using the filter to add your custom userfield data.

---

<div class="post-metadata">

### Author: ![tiagofreitas87](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tiagofreitas87/32/174637_2.png) [@tiagofreitas87](https://meta.discourse.org/u/tiagofreitas87)
#### Post date: [June 29, 2020, 8:47pm UTC](https://meta.discourse.org/t/map-custom-user-fields/156164/4 "2020-06-29T20:47:27Z")

</div>

Thanks! Is there any documentation with all the hooks that you are calling in the plugin?  
Thanks

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [June 29, 2020, 8:50pm UTC](https://meta.discourse.org/t/map-custom-user-fields/156164/5 "2020-06-29T20:50:45Z")

</div>

> [@tiagofreitas87](#):
>
> Is there any documentation with all the hooks that you are calling in the plugin?

No, but there should be. I’ll get that taken care of soon.

---

<div class="post-metadata">

### Author: ![tiagofreitas87](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tiagofreitas87/32/174637_2.png) [@tiagofreitas87](https://meta.discourse.org/u/tiagofreitas87)
#### Post date: [June 29, 2020, 8:53pm UTC](https://meta.discourse.org/t/map-custom-user-fields/156164/6 "2020-06-29T20:53:30Z")

</div>

I need to write a discourse plugin to read the SSO parameters and save then in the user fields, or is that functionality already implemented?

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [June 29, 2020, 10:19pm UTC](https://meta.discourse.org/t/map-custom-user-fields/156164/7 "2020-06-29T22:19:25Z")

</div>

You can update User Fields that you have created on Discourse via SSO. This does not require a plugin. To do this, you need to know the name that Discourse uses in the database for the custom field. The easiest way I know of to do this is by going to your Admin / Customize / User Fields page and then loading the JSON version of the page. For example `https://forum.example.com/admin/customize/user_fields.json`

The JSON data of the page will look something like this:

```plaintext
{
user_fields: [
{
id: 12,
name: "News Letter",
description: "Send me a newsletter",
field_type: "confirm",
editable: false,
required: true,
show_on_profile: false,
show_on_user_card: false,
position: 3
},
{
id: 13,
name: "Company",
description: "Where do you work?",
field_type: "text",
editable: true,
required: false,
show_on_profile: true,
show_on_user_card: true,
position: 4
}
]
}

```

User custom fields are named `user_field_<field_id>`. This means that the name of the “News Letter” field from my JSON example is `user_field_12`, the name of the “Company” field is `user_field_13`. Use this information to update the SSO payload.

As an example, with the `wpdc_sso_params` filter that I mentioned in my previous post, the following code added to my theme’s `functions.php` file will set all users “Company” field to “acme” when they login via SSO. Replace `my_namespace` in the function name with a unique namespace for your site:

```php
add_filter( 'wpdc_sso_params', 'my_namespace_set_discourse_custom_field', 10, 2 );
function my_namespace_set_discourse_custom_field( $sso_params, $user ) {
	$sso_params['custom.user_field_13'] = 'acme';

	return $sso_params;
}

```

To add fields that are based on WordPress user data from your site, use the function’s `$user` parameter to get details about your users.

---

<div class="post-metadata">

### Author: ![tiagofreitas87](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tiagofreitas87/32/174637_2.png) [@tiagofreitas87](https://meta.discourse.org/u/tiagofreitas87)
#### Post date: [June 29, 2020, 10:33pm UTC](https://meta.discourse.org/t/map-custom-user-fields/156164/8 "2020-06-29T22:33:16Z")

</div>

Works perfectly, thanks again!

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [July 29, 2020, 10:33pm UTC](https://meta.discourse.org/t/map-custom-user-fields/156164/9 "2020-07-29T22:33:20Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
