Map custom User Fields

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

1 Like

I think I found how to it, but needs to be changed in wp discourse plugin after every update:

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.

1 Like

I wouldn’t make any direct changes to the plugin’s code unless you are certain about what you are doing. The WP Discourse plugin is built to allow it to be extended by a plugin or a theme by using action hooks. 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:

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

4 Likes

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

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

1 Like

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?

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:

{
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:

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.

3 Likes

Works perfectly, thanks again!

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